home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / LCLINT2.SPK / docs / Guide next >
Text File  |  1996-08-25  |  196KB  |  5,590 lines

  1.  
  2. *** NOTE:  This is a plain text version of the LCLint User's Guide,
  3.            generated from the web page.  Figures are not included,
  4.            and some of the formatting may be unclear.
  5.  
  6.            This version should only be used if none of the other
  7.            versions can be read:
  8.  
  9.        Postscript - guide-part[1,2].ps
  10.        Microsoft Word - guide.doc
  11.        HTML - http://larch-www.lcs.mit.edu:8001/larch/lclint/guide/guide.html
  12.  
  13. *******************
  14.  
  15.                              LCLint User's Guide
  16.  
  17.                                 Version 2.1a
  18.                                  April 1996
  19.  
  20.                                  David Evans
  21.                     Systematic Program Development Group
  22.                      MIT Laboratory for Computer Science
  23.  
  24. LCLint is a tool for statically checking C programs. With minimal effort,
  25. LCLint can be used as a better lint.[1] If additional effort is invested
  26. adding annotations to programs, LCLint can perform stronger checks than can
  27. be done by any standard lint.
  28.  
  29. Some problems detected by LCLint include:
  30.  
  31.    * Violations of information hiding. A user-defined type can be declared
  32.      as abstract, and a message is reported where code inappropriately
  33.      depends on the representation of the type. (Section 3)
  34.  
  35.    * Inconsistent modification of caller-visible state. Functions can be
  36.      annotated with information on what caller-visible state may be modified
  37.      by the function, and an error is reported if the modifications produced
  38.      by the function contradict its declaration. (Section 4.1)
  39.  
  40.    * Inconsistent use of global variables. Information on what global and
  41.      file scope variables a function may use can be added to function
  42.      declarations, and a message is reported if the implementation of the
  43.      function uses other global variables or does not uses every global
  44.      variable listed in its declaration. (Section 4.2)
  45.  
  46.    * Memory management errors. Instances where storage that has been
  47.      deallocated is used, or where storage is not deallocated (memory
  48.      leaks). (Section 5)
  49.  
  50.    * Dangerous data sharing or unexpected aliasing. Parameters to a function
  51.      share storage in a way that may lead to undefined or undesired
  52.      behavior, or a reference to storage within the representation of an
  53.      abstract type is created. (Section 6)
  54.  
  55.    * Using possibly undefined storage or returning storage that is not
  56.      completely defined (except as documented). (Section 7.1)
  57.  
  58.    * Dereferencing a possibly null pointer. (Section 7.2)
  59.  
  60.    * Dangerous macro implementations or invocations. (Section 8)
  61.  
  62.    * Violations of customizable naming conventions. (Section 9)
  63.  
  64.    * Program behavior that is undefined because it depends on order of
  65.      evaluation, likely infinite loops, fall-through cases, incomplete
  66.      logic, statements with no effect, ignored return values, unused
  67.      declarations, and exceeding certain standard limits. (Section 10)
  68.  
  69. LCLint checking can be customized to select what classes of errors are
  70. reported using command line flags and stylized comments in the code.
  71.  
  72. This document is a guide to using LCLint. Section 1 is a brief overview of
  73. the design goals of LCLint. Section 2 explains how to run LCLint, interpret
  74. messages produce, and control checking. Sections 3-10 describe particular
  75. checks done by LCLint.
  76.  
  77. Contents
  78.  
  79.   1. Overview
  80.   2. Operation
  81.         o Messages
  82.         o Flags
  83.         o Stylized Comments
  84.              + Annotations
  85.              + Control Comments
  86.   3. Abstract Types
  87.         o Access
  88.         o Mutability
  89.         o Boulean Types
  90.         o Primitive C Types
  91.              + Characters
  92.              + Enumerators
  93.              + Numeric Types
  94.              + Arbitrary Integral Types
  95.   4. Function Interfaces
  96.         o Modifications
  97.              + Special Modifications
  98.              + Missing Modifies Clauses
  99.              + Limitations
  100.         o Global Variables
  101.              + Controlling Globals Checking
  102.         o Declaration Consistency
  103.   5. Memory Management
  104.         o Storage Model
  105.         o Deallocation Errors
  106.              + Unshared References
  107.              + Temporary Parameters
  108.              + Owned and Dependent References
  109.              + Kept Parameters
  110.              + Stack References
  111.              + Inner Storage
  112.         o Implicit Memory Annotations
  113.         o Reference Counting
  114.   6. Sharing
  115.         o Aliasing
  116.              + Unique Parameters
  117.              + Returned Parameters
  118.         o Exposure
  119.              + Read-Only Storage
  120.              + Exposed Storage
  121.   7. Value Constraints
  122.         o Use Before Definition
  123.              + Undefined Parameters
  124.              + Relaxing Checking
  125.              + Partially Defined Structures
  126.              + Global Variables
  127.         o Null Pointers
  128.              + Predicate Functions
  129.              + Overriding Null Types
  130.              + Relaxing Null Checking
  131.         o Execution
  132.         o Special Clauses
  133.   8. Macros
  134.         o Constant Macros
  135.         o Function-like Macros
  136.              + Side-Effect Free Parameters
  137.              + Polymorphism
  138.         o Controlling Macro Checking
  139.         o Iterators
  140.   9. Naming Conventions
  141.         o Type-Based Naming Conventions
  142.              + Czech Names
  143.              + Slovak Names
  144.              + Czechoslovak Names
  145.         o Namespace Prefixes
  146.         o Naming Restrictions
  147.              + Reserved Names
  148.              + Distinct Identifiers
  149.  10. Other Checks
  150.         o Undefined Evaluation Order
  151.         o Problematic Control Structures
  152.              + Likely Infinite Loops
  153.              + Switches
  154.              + Deep Breaks
  155.              + Loop and If Bodies
  156.              + Complete if-else Logic
  157.         o Suspicious Statements
  158.              + Statements with No Effects
  159.              + Ignored Return Values
  160.         o Unused Declarations
  161.         o Complete Programs
  162.              + Unnecessary External Names
  163.              + Declarations Missing from Headers
  164.         o Compiler Limits
  165.  
  166. Appendices
  167.  
  168.      Appendix A. Availability
  169.      Appendix B. Communication
  170.      Appendix C. Flags
  171.      Appendix D. Annotations
  172.      Appendix E. Control Comments
  173.      Appendix F. Libraries
  174.      Appendix G. Specifications
  175.      Appendix H. Emacs
  176.  
  177. Figures
  178.  
  179. References
  180. Acknowledgements
  181.  
  182. 1. Overview
  183.  
  184. The main goals for LCLint are to:
  185.  
  186.    * Detect a large number of bugs in typical C programs, without producing
  187.      an unacceptable number of spurious messages. We are willing to accept
  188.      the possibility that a few spurious messages are produced as long as it
  189.      enables significantly more powerful checking and the spurious messages
  190.      can be suppressed easily.
  191.  
  192.    * Support a programming methodology involving abstract types and clean,
  193.      documented interfaces in standard C programs.
  194.  
  195.    * Provide a gradual transition for programmers. LCLint can be used like a
  196.      better standard lint with minimal effort. Adding a few annotations to
  197.      programs enables significantly better checking. As more effort is put
  198.      into annotating programs, better checking results. A representational
  199.      effort/benefit curve for using LCLint is shown in Figure 1 (not
  200.      available in HTML format). As different checks are turned on and more
  201.      information is given in code annotations the number of bugs that can be
  202.      detected increases dramatically.
  203.  
  204.    * Provide enough flexibility so that LCLint can be used effectively with
  205.      a wide range of coding styles. Especially important is making it easy
  206.      to use LCLint effectively to maintain and modify legacy code.
  207.  
  208.    * Check programs quickly and with no user interaction. LCLint runs faster
  209.      than most compilers. Libraries can be used to enable fast checking of a
  210.      few modules in a large program.
  211.  
  212. LCLint does many of the traditional lint checks including unused
  213. declarations, type inconsistencies, use-before-definition, unreachable code,
  214. ignored return values, execution paths with no return, likely infinite
  215. loops, and fall-through cases. This document focuses on more powerful checks
  216. that are made possible by additional information given in source code
  217. annotations. [2] Annotations are stylized comments that document certain
  218. assumptions about functions, variables, parameters, and types. They may be
  219. used to indicate where the representation of a user-defined type is hidden,
  220. to limit where a global variable may be used or modified, to constrain what
  221. a function implementation may do to its parameters, and to express checked
  222. assumptions about variables, types, structure fields, function parameters,
  223. and function results. In addition to the checks specifically enabled by
  224. annotations, many of the traditional lint checks are improved by exploiting
  225. this additional information.
  226.  
  227. 2. Operation
  228.  
  229. LCLint is invoked by listing files to be checked. Initialization files,
  230. command line flags, and stylized comments may be used to customize checking
  231. globally and locally.
  232.  
  233. The best way to learn to use LCLint, of course, is to actually use it (if
  234. you don't already have LCLint installed on your system, download it). Before
  235. you read much further in this document, I recommend finding a small C
  236. program. Then, try running:
  237.  
  238. lclint *.c
  239.  
  240. For the most C programs, this will produce a large number of messages. To
  241. turn off reporting for some of the messages, try:
  242.  
  243. lclint -weak *.c
  244.  
  245. The -weak flag is a mode flag that sets many checking parameters to select
  246. weaker checking than is done in the default mode. Other LCLint flags will be
  247. introduced in the following sections; a complete list is given in Appendix
  248. C.
  249.  
  250. 2.1 Messages
  251.  
  252. The format and content of messages printed by LCLint can be customized by
  253. the user. A typical message is:
  254.  
  255.      sample.c: (in function faucet)
  256.      sample.c:11,12: Fresh storage x not released before return
  257.        A memory leak has been detected.  Newly-allocated or only-qualified storage
  258.        is not released before the last reference to is it lost.  Use -mustfree to
  259.        suppress message.
  260.         sample.c:5,47: Fresh storage x allocated
  261.  
  262. The first line gives the name of the function in which the error is found.
  263. This is printed before the first message reported for a function. (The
  264. function context is not printed if -showfunc is used.)
  265.  
  266. The second line is the text of the message. This message reports a memory
  267. leak - storage allocated in a function is not deallocated before the
  268. function returns. The text is preceded by the file name, line and column
  269. number where the error is located. The column numbers are used by the emacs
  270. mode (see Appendix H) to jump to the appropriate line and column location.
  271. (Column numbers are not printed if -showcolumn is used.)
  272.  
  273. The next line is a hint giving more information about the suspected error.
  274. Most hints also include information on how the message may be suppressed.
  275. For this message, setting the -mustfree flag would prevent the message from
  276. being reported. Hints may be turned off by using -hints. Normally, a hint is
  277. given only the first time a class of error is reported. To have LCLint print
  278. a hint for every message regardless, use +forcehints.
  279.  
  280. The final line of the message gives additional location information. For
  281. this message, it tells where the leaking storage is allocated.
  282.  
  283. The generic message format is (parts enclosed in square brackets are
  284. optional):
  285.  
  286.   [<file>:<line> (in <context>)]
  287.   <file>:<line>[,<column>]: message
  288.      [hint]
  289.       <file>:<line>,<column>: extra location information, if appopriate
  290.  
  291. The text of messages and hints may be longer than one line. They are split
  292. into lines of length less than the value set using -linelen <number>. The
  293. default line length is 80 characters. LCLint attempts to split lines in a
  294. sensible place as near to the line length limit as possible.
  295.  
  296. 2.2 Flags
  297.  
  298. So that many programming styles can be supported, LCLint provides over 300
  299. flags for controlling checking and message reporting. Some of the flags are
  300. introduced in the body of this document. Apppendix C describes every flag.
  301. Modes and shortcut flags are provided for setting many flags at once.
  302. Individual flags can override the mode settings.
  303.  
  304. Flags are preceded by + or -. When a flag is preceded by + we say it is on;
  305. when it is preceded by - it is off. The precise meaning of on and off
  306. depends on the type of flag.
  307.  
  308. The +/- flag settings are used for consistency and clarity, but contradict
  309. standard UNIX usage and is easy to accidentally use the wrong one. To reduce
  310. the likelihood of using the wrong flag, LCLint issues warnings when a flag
  311. is set in an unusual way. Warnings are issued when a flag is redundantly set
  312. to the value it already had (these errors are not reported if the flag is
  313. set using a stylized comment), if a mode flag or special flag is set after a
  314. more specific flag that will be set by the general flag was already set, if
  315. value flags are given unreasonable values, of if flags are set in an
  316. inconsistent way. The -warnflags flag suppresses these warnings.
  317.  
  318. Default flag settings will be read from ~/.lclintrc if it is readable. If
  319. there is a .lclintrc file in the working directory, settings in this file
  320. will be read next and its settings will override those in ~/.lclintrc.
  321. Command-line flags override settings in either file. The syntax of the
  322. .lclintrc file is the same as that of command-line flags, except that flags
  323. may be on separate lines and the # character may be used to indicate that
  324. the remainder of the line is a comment. The -nof flag prevents the
  325. ~/.lclintrc file from being loaded. The -f <filename> flag loads options
  326. from filename.
  327.  
  328. 2.3 Stylized Comments
  329.  
  330. Stylized comments are used to provide extra information about a type,
  331. variable or function interface to improve checking, or to control flag
  332. settings locally.
  333.  
  334. All stylized comments begin with /*@ and are closed by the end of the
  335. comment. The role of the @ may be played by any printable character. Use
  336. -commentchar <char> to select a different stylized comment marker.
  337.  
  338. 2.3.1 Annotations
  339.  
  340. Annotations are stylized comments that follow a definite syntax. Although
  341. they are comments, they may only be used in fixed grammatical contexts
  342. (e.g., like a type qualifier).
  343.  
  344. Syntactic comments for function interfaces are described in Section 4;
  345. comments for declaring constants in Section 8.1. and comments for declaring
  346. iterators in Section 8.4. Sections 3-8 include descriptions of annotations
  347. for expressing assumptions about variables, parameters, return values,
  348. structure fields and type definitions. A summary of annotations is found in
  349. Apppendix D.
  350.  
  351. 2.3.2 Control Comments
  352.  
  353. Unlike annotations, control comments may appear between any two tokens in a
  354. C program.[3] Syntactically, they are no different from standard comments.
  355. Control comments are used to provide source-level control of LCLint
  356. checking. They may be used to suppress spurious messages, set flags, and
  357. control checking locally in other ways. A complete description of control
  358. comments is found in Apppendix E.
  359.  
  360. Most flags (all except those characterized as "global" in Apppendix C) can
  361. be set locally using control comments. A control comment can set flags
  362. locally to override the command line settings. The original flag settings
  363. are restored before processing the next file. The syntax for setting flags
  364. in control comments is the same as that of the command line, except that
  365. flags may also be preceded by = to restore their setting to the original
  366. command-line value. For instance,
  367.  
  368. /*@+boolint -modifies =showfunc@*/
  369.  
  370. sets boolint on (this makes bool and int indistinguishable types), sets
  371. modifies off (this prevents reporting of modification errors), and sets
  372. showfunc to its original setting (this controls whether or not the name of a
  373. function is displayed before a message).
  374.  
  375. 3. Abstract Types
  376.  
  377.      Traditionally, programming books wax mathematical when they arrive
  378.      at the topic of abstract data typesŶ Such books make it seem as if
  379.      you'd never actually use an abstract data type except as a sleep
  380.      aid.
  381.  
  382.      - Steve McConnell
  383.  
  384. Information hiding is a technique for handling complexity. By hiding
  385. implementation details, programs can be understood and developed in distinct
  386. modules and the effects of a change can be localized. One technique for
  387. information hiding is data abstraction. An abstract type is used to
  388. represent some natural program abstraction. It provides functions for
  389. manipulating instances of the type. The module that implements these
  390. functions is called the implementation module. We call the functions that
  391. are part of the implementation of an abstract type the operations of the
  392. type. Other modules that use the abstract type are called clients.
  393.  
  394. Clients may use the type name and operations, but should not manipulate or
  395. rely on the actual representation of the type. Only the implementation
  396. module may manipulate the representation of an abstract type. This hides
  397. information, since implementers and maintainers of client modules should not
  398. need to know anything about how the abstract type is implemented. It
  399. provides modularity, since the representation of an abstract type can be
  400. changed without having to change any client code.
  401.  
  402. LCLint supports abstract types by detecting places where client code depends
  403. on the concrete representation of an abstract type.
  404.  
  405. To declare an abstract type, the abstract annotation is added to a typedef.
  406. For example (in mstring.h),
  407.  
  408. typedef /*@abstract@*/ char *mstring;
  409.  
  410. declares mstring as an abstract type. It is implemented using a char *, but
  411. clients of the type should not depend on or need to be aware of this. If it
  412. later becomes apparent that a better representation such as a string table
  413. should be used, we should be able to change the implementation of mstring
  414. without having to change or inspect any client code.
  415.  
  416. In a client module, abstract types are checked by name, not structure.
  417. LCLint reports an error if an instance of mstring is passed as a char * (for
  418. instance, as an argument to strlen), since the correctness of this call
  419. depends on the representation of the abstract type. LCLint also reports
  420. errors if any C operator except assignment (=) or sizeof is used on an
  421. abstract type. The assignment operator is allowed since its semantics do not
  422. depend on the representation of the type.[4] The use of sizeof is also
  423. permitted, since this is the only way for clients to allocate pointers to
  424. the abstract type. Type casting objects to or from abstract types in a
  425. client module is an abstraction violation and will generate a warning
  426. message.
  427.  
  428. Normally, LCLint will assume a type definition is not abstract unless the
  429. /*@abstract@*/ qualifier is used. If instead you want all user-defined types
  430. to be abstract types unless they are marked as concrete, the +impabstract
  431. flag can be used. This adds an implicit abstract annotation to any typedef
  432. that is not marked with /*@concrete@*/.
  433.  
  434. Some examples of abstraction violations detected by LCLint are shown in
  435. Figure 2.
  436.  
  437. 3.1 Access
  438.  
  439. Where code may manipulate the representation of an abstract type, we say the
  440. code has access to that type. If code has access to an abstract type, the
  441. representation of the type and the abstract type are indistinguishable.
  442. Usually, an abstract type is implemented by a single program module that is
  443. the only code that has access to the type representation. Sometimes, more
  444. complicated access control is desired if the implementation of an abstract
  445. type is split across program files, or particular client code needs to
  446. access the representation.
  447.  
  448. There are a several ways of selecting what code has access the
  449. representation of an abstract type:
  450.  
  451.    * Modules. An abstract type defined in M.h is accessible in M.c.
  452.      Controlled by the accessmodule flag. This means when accessmodule is
  453.      on, as it is by default, the module access rule is in effect. If
  454.      accessmodule is off (when -accessmodule is used), the module access
  455.      rule is not in effect and an abstract type defined in M.h is not
  456.      necessarily accessible in M.c
  457.  
  458.    * File names. An abstract type named type is accessible in files named
  459.      type.<extenstion>. For example, the representation of mstring is be
  460.      accessible in mstring.h and mstring.c. Controlled by the accessfile
  461.      flag.
  462.  
  463.    * Function names. An abstract type named type may be accessible in a
  464.      function named type_name or typeName. For example, mstring_length and
  465.      mstringLength would have access to the mstring abstract type.
  466.      Controlled by accessfunction and the naming convention (see Section 9).
  467.  
  468.    * Access control comments. The syntax /*@access type,+@*/[6] allows the
  469.      following code to access the representation of type. Similarly,
  470.      /*@noaccess type,+@*/ restricts access to the representation of type.
  471.      The type in a noaccess comment must have been declared as an abstract
  472.      type.
  473.  
  474. 3.2 Mutability
  475.  
  476. We can view types as being mutable or immutable. A type is mutable if the
  477. value of an instance of the type can be changed by passing it as a parameter
  478. to a function call.[7] For example, the primitive type int is immutable. If
  479. i is a local variable of type int and no variables point to the location
  480. where i is stored, the value of i must be the same before and after the call
  481. f(i). Structure and union types are also immutable, since they are copied
  482. when they are passed as arguments. On the other hand, pointer types are
  483. mutable. If x is a local variable of type int *, the value of *x (and hence,
  484. the value of the object x) can be changed by the function call g(x).
  485.  
  486. The mutability of a concrete type is determined by its type definition. For
  487. abstract types, mutability does not depend on the type representation but on
  488. what operations the type provides. If an abstract type has operations that
  489. may change the value of instances of the type, the type is mutable. If not,
  490. it is immutable. The value of an instance of an immutable type never
  491. changes. Since object sharing is noticeable only for mutable types, they are
  492. checked differently from immutable types.
  493.  
  494. The /*@mutable@*/ and /*@immutable@*/ annotations are used to declare an
  495. abstract type as mutable or immutable. (If neither is used, the abstract
  496. type is assumed to be mutable.) For example,
  497.  
  498. typedef /*@abstract@*/ /*@mutable@*/ char *mstring; typedef /*@abstract@*/
  499. /*@immutable@*/ int weekDay; declares mstring as a mutable abstract type and
  500. weekDay as an immutable abstract type.
  501.  
  502. Clients of a mutable abstract type need to know the semantics of assignment.
  503. After the assignment expression s = t, do s and t refer to the same object
  504. (that is, will changes to the value of s also change the value of t)?
  505.  
  506. LCLint prescribes that all abstract types have sharing semantics, so s and t
  507. would indeed be the same object. LCLint will report an error if a mutable
  508. type is implemented with a representation (e.g., a struct) that does not
  509. provide sharing semantics (controlled by mutrep flag).
  510.  
  511. The mutability of an abstract type is not necessarily the same as the
  512. mutability of its representation. We could use the immutable concrete type
  513. int to represent mutable strings using an index into a string table, or
  514. declare mstring as immutable as long as no operations are provided that
  515. modify the value of an mstring.
  516.  
  517. 3.3 Boolean Types
  518.  
  519. Standard C has no boolean representation - the result of a comparison
  520. operator is an integer, and no type checking is done for test expressions.
  521. Many common errors can be detected by introducing a distinct boolean type
  522. and stronger type checking.
  523.  
  524. By convention, the bool type is used to represent boolean values.[8]
  525. Relations, comparisons and certain standard library functions are declared
  526. to return bool types.
  527.  
  528. LCLint checks that the test expression in an if, while, or for statement or
  529. an operand to &&, || or ! is a boolean. If the type of a test expression is
  530. not a boolean, LCLint will report an error depending on the type of the test
  531. expression and flag settings. If the test expression has pointer type,
  532. LCLint reports an error if predboolptr is on (this can be used to prevent
  533. messages for the idiom of testing if a pointer is not null without a
  534. comparison). If it is type int, an error is reported if predboolint is on.
  535. For all other types, LCLint reports an error if predboolothers is on.
  536.  
  537. Since using = instead of == is such a common bug, reporting of test
  538. expressions that are assignments is controlled by the separate predassign
  539. flag. The message can be suppressed by adding extra parentheses around the
  540. test expression.
  541.  
  542. Apppendix C (page 50) describes other flags for controlling boolean
  543. checking.
  544.  
  545. Figure 3. Boolean Checking
  546.  
  547. 3.4 Primitive C Types
  548.  
  549.      Two types have compatible type if their types are the same.
  550.      - ANSI C, 3.1.2.6.
  551.  
  552.      Two types need not be identical to be compatible.
  553.      - ANSI C, footnote to 3.1.2.6.
  554.  
  555. LCLint supports stricter checking of primitive C types. The char and enum
  556. types can be checked as distinct types, and the different numeric types can
  557. be type-checked strictly.
  558.  
  559. 3.4.1 Characters
  560.  
  561. The primitive char type can be type-checked as a distinct type. If char is
  562. used as a distinct type, common errors involving assigning ints to chars are
  563. detected.
  564.  
  565. The +charint flag can be used for checking legacy programs where char and
  566. int are used interchangeably. If charint is on, char types indistinguishable
  567. from ints. To keep char and int as distinct types, but allow chars to be
  568. used to index arrays, use +charindex.
  569.  
  570. 3.4.2 Enumerators
  571.  
  572. Standard C treats user-declared enum types just like integers. An arbitrary
  573. integral value may be assigned to an enum type, whether or not it was listed
  574. as an enumerator member. LCLint checks each user-defined enum type as
  575. distinct type. An error is reported if a value that is not an enumerator
  576. member is assigned to the enum type, or if an enum type is used as an
  577. operand to an arithmetic operator.
  578.  
  579. If the enumint flag is on, enum and int types may be used interchangeably.
  580. Like charindex, if the enumindex flag is on, enum types may be used to index
  581. arrays.
  582.  
  583. 3.4.3 Numeric Types
  584.  
  585. LCLint reports where numeric types are used in dangerous or inconsistent
  586. ways. With the strictest checking, LCLint will report an error anytime
  587. numeric types do not match exactly. If the relaxquals flag is on, only those
  588. inconsistencies which may corrupt values are reported. For example, if an
  589. int is assigned to a variable of type long (or passed as a long formal
  590. parameter), LCLint will not report an error if relaxquals is on since a long
  591. must have at least enough bits to store an int without data loss. On the
  592. other hand, an error would be reported if the long were assigned to an int,
  593. since the int type may not have enough bits to store the long value.
  594.  
  595. Similarly, if a signed value is assigned to an unsigned, LCLint will report
  596. an error since an unsigned type cannot represent all signed values
  597. correctly. If the ignoresigns flag is on, checking is relaxed to ignore all
  598. sign qualifiers in type comparisons (this is not recommended, since it will
  599. suppress reporting of real bugs, but may be necessary for quickly checking
  600. certain legacy code).
  601.  
  602. 3.4.4 Arbitrary Integral Types
  603.  
  604. The standard library declares the types size_t, ptr_diff and wchar_t . The
  605. standard does not constrain their types other than limiting them to integral
  606. types. An arbitrary integral type is used to represent these library types.
  607. The matchanyintegral, longintegral, and longunsignedanyintegral flags
  608. control type checking for the arbitrary integral type. If none of these
  609. flags is on, a type mismatch is reported anywhere an arbitrary integral type
  610. is used where a specific (possibly inconsistent) integral type is expected.
  611. If matchanyintegral is on, an arbitrary integral type is allowed to match
  612. any integral type. If longintegral or longunsignedintegral is on, an
  613. arbitrary integral type match a long or long unsigned type, respectively.
  614.  
  615. 4. Function Interfaces
  616.  
  617. Functions communicate with their calling environment through an interface.
  618. The caller communicates the values of actual parameters and global variables
  619. to the function, and the function communicates to the caller through the
  620. return value, global variables and storage reachable from the actual
  621. parameters. By keeping interfaces narrow (i.e., restricting the amount of
  622. information visible across a function interface), we can understand and
  623. implement functions independently.
  624.  
  625. A function prototype documents the interface to a function. It serves as a
  626. contract between the function and its caller. In early versions of C, the
  627. function "prototype" was very limited. It described the type returned by the
  628. function but nothing about its parameters. The main improvement provided by
  629. ANSI C was the ability to add information on the number and types of
  630. parameter to a function. LCLint provides the means to express much more
  631. about a function interface: what global variable the function may use, what
  632. values visible to the caller it may modify, if a pointer parameter may be a
  633. null pointer or point to undefined storage, if storage pointed to by a
  634. parameter is deallocated before the function returns, if the function may
  635. create new aliases to a parameter, can the caller modify or deallocate the
  636. return value, etc.
  637.  
  638. The extra interface information places constraints on both how the function
  639. may be called and how it may be implemented. LCLint reports places where
  640. these constrains are not satisfied. Typically, these indicate bugs in the
  641. code or errors in the interface documentation.
  642.  
  643. This section describes syntactic comments may be added to a function
  644. declaration to document what global variables the function implementation
  645. may use and what values visible to its caller it may modify. Sections 5-7
  646. describe annotations may be added to parameters to constrain valid arguments
  647. to a function and how these arguments may be used after the call and to the
  648. return value to constrain results.
  649.  
  650. 4.1 Modifications
  651.  
  652. The modifies clause lists what values visible to the caller may be modified
  653. by a function. Modifies clauses limit what values a function may modify, but
  654. they do not require that listed values are always modified. The declaration,
  655.  
  656. int f (int *p, int *q) /*@modifies *p@*/;
  657.  
  658. declares a function f that may modify the value pointed to by its first
  659. argument but may not modify the value of its second argument or any global
  660. state.
  661.  
  662. LCLint checks that a function does not modify any caller-visible value not
  663. encompassed by its modifies clause and does modify all values listed in its
  664. modifies clause on some possible execution of the function. Figure 4 shows
  665. an example of modifies checking done by LCLint.
  666.  
  667. 4.1.1 Special Modifications
  668.  
  669. A few special names are provided for describing function modifications:
  670.  
  671. internalState
  672.  
  673.      The function modifies some internal state (that is, the value of a
  674.      static variable). Even though a client cannot access the internal
  675.      state directly, it is important to know that something may be
  676.      modified by the function call both for clear documentation and for
  677.      checking undefined order of evaluation (Section 10.1) and
  678.      side-effect free parameters (Section 8.2.1).
  679.  
  680. fileSystem
  681.  
  682.      The function modifies the file system (e.g., writes to a file).
  683.  
  684. nothing
  685.  
  686.      The function modifies nothing (i.e., it is side-effect free).
  687.  
  688. The syntactic comment, /*@*/ in a function declaration or definition (after
  689. the parameter list, before the semi-colon or function body) denotes a
  690. function that modifies nothing and does not use any global variables (see
  691. Section 4.2).
  692.  
  693. Figure 4. Modifies checking.
  694.  
  695. 4.1.2 Missing Modifies Clauses
  696.  
  697. LCLint is designed so programs with many functions that are declared without
  698. modifies clauses can be checked effectively. Unless modnomods is in on, no
  699. modification errors are reported checking a function declared with no
  700. modifies clause.
  701.  
  702. A function with no modifies clause is an unconstrained function since there
  703. are no documented constraints on what it may modify. When an unconstrained
  704. function is called, it is checked differently from a function declared with
  705. a modifies clause. To prevent spurious errors, no modification error is
  706. reported at the call site unless the moduncon flag is on. Flags control
  707. whether errors involving unconstrained functions are reported for other
  708. checks that depend on modifications (side-effect free macro parameters
  709. (Section 8.2.1), undefined evaluation order (Section 10.1), and likely
  710. infinite loops (Section 10.2.1).)
  711.  
  712. 4.1.3 Limitations
  713.  
  714. Determining whether a function modifies a particular parameter or global is
  715. in general an undecidable[9] problem. To enable useful checking, certain
  716. simplifying assumptions are necessary. LCLint assumes an object is modified
  717. when it appears on the left hand side of an assignment or it is passed to a
  718. function as a parameter which may be modified by that function (according to
  719. the called function's modifies clause). Hence, LCLint will report spurious
  720. modification errors for assignments that do not change the value of an
  721. object or modifications that are always reversed before a procedure returns.
  722. The /*@-mods@*/ and /*@=mods@*/ control comments can be used around these
  723. modifications to suppress the message.
  724.  
  725. 4.2 Global Variables
  726.  
  727. Another aspect of a function's interface, is the global variables it uses. A
  728. globals list in a function declaration lists external variables that may be
  729. used in the function body. LCLint checks that global variables used in a
  730. procedure match those listed in its globals list. A global is used in a
  731. function if it appears in the body directly, or it is in the globals list of
  732. a function called in the body. LCLint reports if a global that is used in a
  733. procedure is not listed in its globals list, and if a listed global is not
  734. used in the function implementation.
  735.  
  736. Figure 5 shows an example function definition with a globals list and
  737. associated checking done by LCLint.
  738.  
  739. 4.2.1 Controlling Globals Checking
  740.  
  741. Whether on not an error is reported for a use of a global variable in a
  742. given function depends on the scope of the variable (file static or
  743. external), the checking annotation used in the variable declaration or the
  744. implicit annotation if no checking annotation is used, whether or not the
  745. function is declared with a globals list, and flag settings.
  746.  
  747. A global or file static variable declaration may be preceded by an
  748. annotation to indicate how the variable should be checked. In order of
  749. decreasing checks, the annotations are:
  750.  
  751. /*@checkedstrict@*/
  752.  
  753.      Strictest checking. Undocumented uses and modifications of the
  754.      variable are reported in all functions whether or not they have a
  755.      globals list (unless checkstrictglobs is off).
  756.  
  757. /*@checked@*/
  758.  
  759.      Undocumented use of the variable is reported in a function with a
  760.      globals list, but not in a function declared with no globals
  761.      (unless globnoglobs is on).
  762.  
  763. /*@checkmod@*/
  764.  
  765.      Undocumented uses of the variable are not reported, but
  766.      undocumented modifications are reported. (If modglobsnomods is on,
  767.      errors are reported even in functions declared with no modifies
  768.      clause or globals list.)
  769.  
  770. /*@unchecked@*/
  771.  
  772.      No messages are reported for undocumented use or modification of
  773.      this global variable. If a variable has none of these annotations,
  774.      an implicit annotation is determined by the flag settings.
  775.  
  776. Different flags control the implicit annotation for variables declared with
  777. global scope and variables declared with file scope (i.e., using the static
  778. storage qualifier). To set the implicit annotation for global variables
  779. declared in context (globs for external variables or statics for file static
  780. variable) to be annotation (checked, checkmod, checkedstrict) use
  781. imp<annotation><context>. For example, +impcheckedstrictstatics makes the
  782. implicit checking on unqualified file static variables checkedstrict. (See
  783. Apppendix C, page 51, for a complete list of globals checking flags.)
  784.  
  785. 4.3 Declaration Consistency
  786.  
  787. LCLint checks that function declarations and definitions are consistent. The
  788. general rule is that the first declaration of a function imply all later
  789. declarations and definitions. If a function is declared in a header file,
  790. the first declaration processed is its first declaration (if it is declared
  791. in more than one header file an error is reported if redecl is set).
  792. Otherwise, the first declaration in the file defining the function is its
  793. first declaration.
  794.  
  795. Later declarations may not include variables in the globals list that were
  796. not included in the first declaration. The exception to this is when the
  797. first declaration is in a header file and the later declaration or
  798. definition includes file static variables. Since these are not visible in
  799. the header file, they can not be included in the header file declaration.
  800. Similarly, the modifies clause of a later declaration may not include
  801. objects that are not modifiable in the first declaration. The later
  802. declaration may be more specific. For example, if the header declaration is:
  803.  
  804. extern void setName (employee e, char *s) /*@modifies e@*/;
  805.  
  806. the later declaration could be,
  807.  
  808.    void setName (employee e, char *) /*@modifies e->name@*/;
  809.  
  810. If employee is an abstract type, the declaration in the header should not
  811. refer to a particular implementation (i.e., it shouldn't rely on there being
  812. a name field), but the implementation declaration can be more specific.
  813.  
  814. This rule also applies to file static variables. The header declaration for
  815. a function that modifies a file static variable should use modifies
  816. internalState since file static variables are not visible to clients. The
  817. implementation declaration should list the actual file static variables that
  818. may be modified.
  819.  
  820. 5. Memory Management
  821.  
  822. About half the bugs in typical C programs can be attributed to memory
  823. management problems. Memory management bugs are notoriously difficult to
  824. detect through traditional techniques. Often, the symptom of the bug is far
  825. removed from its actual source. Memory management bug often only appear
  826. sporadically and some bugs may only be apparent when compiler optimizations
  827. are turned on or the code is compiled on a different platform. Run-time
  828. tools offer some help, but are cumbersome to use and limited to detecting
  829. errors that occur when test cases are run. By detecting these errors
  830. statically, we can be confident that certain types of errors will never
  831. occur and provide verified documentation on the memory management behavior
  832. of a program.
  833.  
  834. LCLint can detect many memory management errors at compile time including:
  835.  
  836.    * using storage that may have been freed (Section 5.2)
  837.  
  838.    * failing to deallocate memory (Section 5.2)
  839.  
  840.    * returning a pointer to stack-allocated storage (Section 5.2.6)
  841.  
  842.    * undocumented or dangerous aliasing or storage sharing (Section 6)
  843.  
  844.    * passing or returning storage that is not completely defined (Section
  845.      7.1)
  846.  
  847.    * dereferencing a null pointers (Section 7.2)
  848.  
  849. Most of these checks rely heavily on annotations added to programs to
  850. document assumptions related to memory management and pointer values. By
  851. documenting these assumptions for function interfaces, variables, type
  852. definitions and structure fields, memory management bugs can be detected at
  853. their source -- where an assumption is violated. In addition, precise
  854. documentation about memory management decisions makes its easier to change
  855. code.
  856.  
  857. 5.1 Storage Model[10]
  858.  
  859.      Yea, from the table of my memory I'll wipe away all trivial fond
  860.      records, all saws of books, all forms, all pressures past, that
  861.      youth and observation copied there.
  862.      - Hamlet prefers garbage collection (Shakespeare, Hamlet. Act I,
  863.      Scene v)
  864.  
  865. This section describes execution-time concepts for describing the state of
  866. storage more precisely than can be done using standard C terminology.
  867. Certain uses of storage are likely to indicate program bugs, and are
  868. reported as anomalies.
  869.  
  870. LCL assumes a CLU-like object storage model.[11] An object is a typed region
  871. of storage. Some objects use a fixed amount of storage that is allocated and
  872. deallocated automatically by the compiler.
  873.  
  874. Other objects use dynamic storage that must be managed by the program.
  875.  
  876. Storage is undefined if it has not been assigned a value, and defined after
  877. it has been assigned a value. An object is completely defined if all storage
  878. that may be reached from it is defined. What storage is reachable from an
  879. object depends on the type and value of the object. For example, if p is a
  880. pointer to a structure, p is completely defined if the value of p is NULL,
  881. or if every field of the structure p points to is completely defined.
  882.  
  883. When an expression is used as the left side of an assignment expression we
  884. say it is used as an lvalue. Its location in memory is used, but not its
  885. value. Undefined storage may be used as an lvalue since only its location is
  886. needed. When storage is used in any other way, such as on the right side of
  887. an assignment, as an operand to a primitive operator (including the
  888. indirection operator, *),[12] or as a
  889.  
  890. function parameter, we say it is used as an rvalue. It is an anomaly to use
  891. undefined storage as an rvalue.
  892.  
  893. A pointer is a typed memory address. A pointer is either live or dead. A
  894. live pointer is either NULL or an address within allocated storage. A
  895. pointer that points to an object is an object pointer. A pointer that points
  896. inside an object (e.g., to the third element of an allocated block) is an
  897. offset pointer. A pointer that points to allocated storage that is not
  898. defined is an allocated pointer. The result of dereferencing an allocated
  899. pointer is undefined storage. Hence, it is an anomaly to use it as an
  900. rvalue. A dead (or "dangling") pointer does not point to allocated storage.
  901. A pointer becomes dead if the storage it points to is deallocated (e.g., the
  902. pointer is passed to the free library function.) It is an anomaly to use a
  903. dead pointer as an rvalue.
  904.  
  905. There is a special object null corresponding to the NULL pointer in a C
  906. program. A pointer that may have the value NULL is a possibly-null pointer.
  907. It is an anomaly to use a possibly-null pointer where a non-null pointer is
  908. expected (e.g., certain function arguments or the indirection operator).
  909.  
  910. 5.2 Deallocation Errors
  911.  
  912. There are two kinds of deallocation errors with which we are concerned:
  913. deallocating storage when there are other live references to the same
  914. storage, or failing to deallocate storage before the last reference to it is
  915. lost. To handle these deallocation errors, we introduce a concept of an
  916. obligation to release storage. Every time storage is allocated, it creates
  917. an obligation to release the storage. This obligation is attached to the
  918. reference to which the storage is assigned.[13] Before the scope of the
  919. reference is exited or it is assigned to a new value, the storage to which
  920. it points must be released. Annotations can be used to indicate that this
  921. obligation is transferred through a return value, function parameter or
  922. assignment to an external reference.
  923.  
  924. 5.2.1 Unshared References
  925.  
  926.      `Tis in my memory lock'd, and you yourself shall keep the key of
  927.      it.
  928.      - Ophelia prefers explicit deallocation (Hamlet. Act I, Scene iii)
  929.  
  930. The only annotation is used to indicate a reference is the only pointer to
  931. the object it points to. We can view the reference as having an obligation
  932. to release this storage. This obligation is satisfied by transferring it to
  933. some other reference in one of three ways:
  934.  
  935.    * pass it as an actual parameter corresponding to a formal parameter
  936.      declared with an only annotation
  937.    * assign it to an external reference declared with an only annotation
  938.    * return it as a result declared with an only annotation
  939.  
  940. After the release obligation is transferred, the original reference is a
  941. dead pointer and the storage it points to may not be used.
  942.  
  943. All obligations to release storage stem from primitive allocation routines
  944. (e.g., malloc), and are ultimately satisfied by calls to free. The standard
  945. library declared the primitive allocation and deallocation routines.
  946.  
  947. The basic memory allocator, malloc, is declared:[14]
  948.  
  949. /*@only@*/ void *malloc (size_t size);
  950.  
  951. It returns an object that is referenced only by the function return value.
  952.  
  953. The deallocator, free, is declared:[15]
  954.  
  955. void free (/*@only@*/ void *ptr);
  956.  
  957. The parameter to free must reference an unshared object. Since the parameter
  958. is declared using only, the caller may not use the referenced object after
  959. the call, and may not pass in a reference to a shared object. There is
  960. nothing special about malloc and free -- their behavior can be described
  961. entirely in terms of the provided annotations.
  962.  
  963. Figure 6. Deallocation errors.
  964.  
  965. 5.2.2 Temporary Parameters
  966.  
  967. The temp annotation is used to declare a function parameter that is used
  968. temporarily by the function. An error is reported if the function releases
  969. the storage associated with a temp formal parameter or creates new aliases
  970. it that are visible after the function returns. Any storage may be passed as
  971. a temp parameter, and it satisfies its original memory constraints after the
  972. function returns.
  973.  
  974. 5.2.3 Owned and Dependent References
  975.  
  976. In real programs it is sometimes necessary to have storage that is shared
  977. between several possibly references. The owned and dependent annotations
  978. provide a more flexible way of managing storage, at the cost of less
  979. checking. The owned annotation denotes a reference with an obligation to
  980. release storage. Unlike only, however, other external references marked with
  981. dependent annotations may share this object. It is up to the programmer to
  982. ensure that the lifetime of a dependent reference is contained within the
  983. lifetime of the corresponding owned reference.
  984.  
  985. 5.2.4 Kept Parameters
  986.  
  987. The keep annotation is similar to only, except the caller may use the
  988. reference after the call. The called function must assign the keep parameter
  989. to an only reference, or pass it as a keep parameter to another function. It
  990. is up to the programmer to make sure that the calling function does not use
  991. this reference after it is released. The keep annotation is useful for
  992. adding an object to a collection (e.g., a symbol table), where it is known
  993. that it will not be deallocated until the collection is.
  994.  
  995. 5.2.5 Shared References
  996.  
  997. If LCLint is used to check a program designed to be used in a
  998. garbage-collected environment, there may be storage that is shared by one or
  999. more references and never explicitly released. The shared annotation
  1000. declares storage that may be shared arbitrarily, but never released.
  1001.  
  1002. 5.2.6 Stack References
  1003.  
  1004. Local variables that are not allocated dynamically are store on a call
  1005. stack. When a function returns, its stack frame is deallocated, destroying
  1006. the storage associated with the function's local variables. A memory error
  1007. occurs if a pointer into this storage is live after the function returns.
  1008. LCLint detects errors involving stack references exported from a function
  1009. through return values or assignments to references reachable from global
  1010. variables or actual parameters. No annotations are needed to detect stack
  1011. reference errors, since it is clear from a declaration if storage is
  1012. allocated on the function stack.
  1013.  
  1014. Figure 7. Stack references.
  1015.  
  1016. 5.2.7 Inner Storage
  1017.  
  1018. An annotation always applies to the outermost level of storage. For example,
  1019.  
  1020. /*@only@*/ int **x;
  1021.  
  1022. declares x as an unshared pointer to a pointer to an int. The only
  1023. annotation applies to x, but not to *x. To apply annotations to inner
  1024. storage a type definition may be used:
  1025.  
  1026.   typedef /*@only@*/ int *oip;
  1027.   /*@only@*/ oip *x;
  1028.  
  1029. Now, x is an only pointer to an oip, which is an only pointer to an int.
  1030.  
  1031. When annotations are use in type definitions, they may be overridden in
  1032. instance declarations. For example,
  1033.  
  1034. /*@dependent@*/ oip x;
  1035.  
  1036. makes x a dependent pointer to an int.
  1037.  
  1038. 5.3 Implicit Memory Annotations
  1039.  
  1040. Since it is important that LCLint can check unannotated programs
  1041. effectively, the meaning of declarations with no memory annotations is
  1042. chosen to minimize the number of annotations needed to get useful checking
  1043. on an unannotated program.
  1044.  
  1045. An implicit memory management annotation may be assumed for declarations
  1046. with no explicit memory management annotation. Implicit annotations are
  1047. checked identically to the corresponding explicit annotation, except error
  1048. messages indicate that they result from an implicit annotation.
  1049.  
  1050. Unannotated function parameters are assumed to be temp. This means if memory
  1051. checking is turned on for an unannotated program, all functions that release
  1052. storage referenced by a parameter or assign a global variable to alias the
  1053. storage will produce error messages. (Controlled by paramimptemp.)
  1054.  
  1055. Unannotated return values, structure fields and global variables are assumed
  1056. to be only. With implicit annotations (on by default), turning on memory
  1057. checking for an unannotated program will produce errors for any function
  1058. that does not return unshared storage or assignment of shared storage to a
  1059. global variable or structure field.[16] (Controlled by retimponly,
  1060. structimponly and globimponly. The allimponly flag sets all of the implicit
  1061. only flags.)
  1062.  
  1063. Figure 8. Implicit annotations
  1064.  
  1065. 5.4 Reference Counting
  1066.  
  1067. Another approach to memory management is to add a field to a type to
  1068. explicitly keep track of the number of references to that storage. Every
  1069. time a reference is added or lost the reference count is adjusted
  1070. accordingly; if it would become zero, the storage is released. Reference
  1071. counting it difficult to do without automatic checking since it is easy to
  1072. forget to increment or decrement the reference count, and exceedingly
  1073. difficult to track down these errors.
  1074.  
  1075. LCLint supports reference counting by using annotations to constrain the use
  1076. of reference counted storage in a manner similar to other memory management
  1077. annotations.
  1078.  
  1079. A reference counted type is declared using the refcounted annotation. Only
  1080. pointer to struct types may be declared as reference counted, since
  1081. reference counted storage must have a field to count the references. One
  1082. field in the structure (or integral type) is preceded by the refs annotation
  1083. to indicate that the value of this field is the number of live references to
  1084. the structure.
  1085.  
  1086. For example (in rstring.h),
  1087.  
  1088.      typedef /*@abstract@*/ /*@refcounted@*/ struct {
  1089.           /*@refs@*/ int refs;
  1090.         char *contents;
  1091.       } *rstring;
  1092.  
  1093. declares rstring as an abstract, reference-counted type. The refs field
  1094. counts the number of references and the contents field holds the contents of
  1095. a string.
  1096.  
  1097. All functions that return refcounted storage must increase the reference
  1098. count before returning. LCLint cannot determine if the reference count was
  1099. increased, so any function that directly returns a reference to refcounted
  1100. storage will produce an error. This is avoided, by using a function to
  1101. return a new reference (e.g., rstring_ref in Figure 9).
  1102.  
  1103. A reference counted type may be passed as a temp or dependent parameter. It
  1104. may not be passed as an only parameter. Instead, the killref annotation is
  1105. used to denote a parameter whose reference is eliminated by the function
  1106. call. Like only parameters, an actual parameter corresponding to a killref
  1107. formal parameter may not be used in the calling function after the call.
  1108. LCLint checks that the implementation of a function releases all killref
  1109. parameters, either by passing them as killref parameters, or assigning or
  1110. returning them without increasing the reference count.
  1111.  
  1112. Figure 9. Reference counting.
  1113.  
  1114. 6. Sharing
  1115.  
  1116. Errors involving unexpected sharing of storage can cause serious problems.
  1117. Undocumented sharing may lead to unpredictable modifications, and some
  1118. library calls (e.g., strcpy) have undefined behavior if parameters share
  1119. storage. Another class of sharing errors occurs when clients of an abstract
  1120. type may obtain a reference to mutable storage that is part of the abstract
  1121. representation. This exposes the representation of the abstract type, since
  1122. clients may modify an instance of the abstract type indirectly through this
  1123. shared storage.
  1124.  
  1125. 6.1 Aliasing
  1126.  
  1127. LCLint detects errors involving dangerous aliasing of parameters. Some of
  1128. these errors are already detected through the standard memory annotations
  1129. (e.g., only parameters may not be aliases.) Two additional annotations are
  1130. provided for constraining aliasing of parameters and return values.
  1131.  
  1132. 6.1.1 Unique Parameters
  1133.  
  1134. The unique annotation denotes a parameter that may not be aliased by any
  1135. other storage reachable from the function implementation -- that is, any
  1136. storage reachable through the other parameters or global variables used by
  1137. the function. The unique annotation places similar constraints on function
  1138. parameters as the only annotation, but it does not transfer the obligation
  1139. to release storage.
  1140.  
  1141. LCLint will report an error if a unique parameter may be aliased by another
  1142. parameter or global variable.
  1143.  
  1144. Figure 10. Unique parameters.
  1145.  
  1146. 6.1.2 Returned Parameters
  1147.  
  1148. LCLint reports an error if a function returns a reference to storage
  1149. reachable from one of its parameters (if retalias is on) since this may
  1150. introduce unexpected aliases in the body of the calling function when the
  1151. result is assigned.
  1152.  
  1153. The returned annotation denotes a parameter that may be aliased by the
  1154. return value. LCLint checks the call assuming the result may be an alias to
  1155. the returned parameter. Figure 11 shows an example use of a returned
  1156. annotation.
  1157.  
  1158. 6.2 Exposure
  1159.  
  1160. LCLint detects places where the representation of an abstract type is
  1161. exposed. This occurs if a client has a pointer to storage that is part of
  1162. the representation of an instance of the abstract type. The client can then
  1163. modify or examine the storage this points to, and manipulate the value of
  1164. the abstract type instance without using its operations.
  1165.  
  1166. There are three ways a representation may be exposed:
  1167.  
  1168.   1. Returning (or assigning to a global variable) an object that includes a
  1169.      pointer to a mutable component of an abstract type representation.
  1170.      (Controlled by retexpose).
  1171.   2. Assigning a mutable component of an abstract object to storage
  1172.      reachable from an actual parameter or a global variable that may be
  1173.      used after the call. This means the client may manipulate the abstract
  1174.      object using the actual parameter after the call. Note that if the
  1175.      corresponding formal parameter is declared only, the caller may not use
  1176.      the actual parameter after the call so the representation is not
  1177.      exposed. (Controlled by assignexpose).
  1178.   3. Casting mutable storage to or from an abstract type. (Controlled by
  1179.      castexpose).
  1180.  
  1181. Annotations may be used to allow exposed storage to be returned safely by
  1182. restricting how the caller may use the returned storage.
  1183.  
  1184. 6.2.1 Read-Only Storage
  1185.  
  1186. It is often useful for a function to return a pointer to internal storage
  1187. (or a instance of a mutable abstract type) that is intended only as an
  1188. observer. The caller may use the result, but should not modify the storage
  1189. it points to. For example, consider an naïve implementation of the
  1190. employee_getName operation for the abstract employee type:
  1191.  
  1192.    typedef /*@abstract@*/ struct {
  1193.       char *name;
  1194.       int id;
  1195.    } *employee;
  1196.    ...
  1197.    char *employee_getName (employee e) { return e->name; }
  1198.  
  1199. LCLint produces a message to indicate that the return value exposes the
  1200. representation. One solution would be to return a fresh copy of e->name.
  1201. This is expensive, though, especially if we expect employee_getName is used
  1202. mainly just to get a string for searching or printing. Instead, we could
  1203. change the declaration of employee_getName to:
  1204.  
  1205. extern /*@observer@*/ char *employee_getName (employee e);
  1206.  
  1207. Now, the original implementation is correct. The declaration indicates that
  1208. the result may not be modified by the caller, so it is acceptable to return
  1209. shared storage.[17] LCLint checks that the return value is not modified by
  1210. the caller. An error is reported if observer storage is modified directly,
  1211. passed as a function parameter that may be modified, assigned to a global
  1212. variable or reference derivable from a global variable that is not declared
  1213. with an observer annotation, or returned as a function result or a reference
  1214. derivable from the function result that is not annotation with an observer
  1215. annotation.
  1216.  
  1217. String Literals
  1218.  
  1219. A program that attempts to modify a string literal has undefined behavior
  1220. [ANSI, Section 3.1.4]. This is not enforced by most C compilers, and can
  1221. lead to particularly pernicious bugs that only appear when optimizations are
  1222. turned on and the compiler attempts to minimize storage for string literals.
  1223. LCLint can be used to check that string literals are not modified, by
  1224. treating them as observer storage. If readonlystrings is on (default in
  1225. standard mode), LCLint will report an error if a string literal is modified.
  1226.  
  1227. 6.2.2 Exposed Storage
  1228.  
  1229. Sometimes it is necessary to expose the representation of an abstract type.
  1230. This may be evidence of a design flaw, but in some cases is justified for
  1231. efficiency reasons. The exposed annotation denotes storage that is exposed.
  1232. It may be used on a return value for results that reference storage internal
  1233. to an abstract representation, on a parameter value to indicate a parameter
  1234. that may be assigned directly to part of an abstract representation,[18] or
  1235. on a field of an abstract representation to indicate that external
  1236. references to the storage may exist. An error is reported if exposed storage
  1237. is released, but unlike an observer, no error is reported if it is modified.
  1238.  
  1239. Figure 12 shows examples of exposure problems detected by LCLint.
  1240.  
  1241. 7. Value Constraints
  1242.  
  1243. LCLint can be used to constrain values of parameters, function results,
  1244. global variables, and derived storage such as structure fields. These
  1245. constraints are checked at interface points -- where a function is called or
  1246. returns. Section 7.1 describes how to constrain parameters, return values
  1247. and structures to detect use before definition errors. A similar approach is
  1248. used for restricting the use of possibly null pointers in Section 7.2. To do
  1249. both well, and avoid spurious errors, information about when and if a
  1250. function returns if useful. Annotations for documenting execution control
  1251. are described in Section 7.3.
  1252.  
  1253. 7.1 Use Before Definition
  1254.  
  1255. Like many static checkers, LCLint detects instances where the value of a
  1256. location is used before it is defined. This analysis is done at the
  1257. procedural level. If there is a path through the procedure that
  1258.  
  1259. uses a local variable before it is defined, a use before definition error is
  1260. reported. Use before definition checking is controlled by the usedef flag.
  1261.  
  1262. LCLint can do more checking than standard checkers though, because the
  1263. annotations can be used to describe what storage must be defined and what
  1264. storage may be undefined at interface points. Unannotated references are
  1265. expected to be completely defined at interface points. This means all
  1266. storage reachable from a global variable, parameter to a function, or
  1267. function return value is defined before and after a function call.
  1268.  
  1269. 7.1.1 Undefined Parameters
  1270.  
  1271. Sometimes, function parameters or return values are expected to reference
  1272. undefined or partially defined storage. For example, a pointer parameter may
  1273. be intended only as an address to store a result, or a memory allocator may
  1274. return allocated but undefined storage. The out annotation denotes a pointer
  1275. to storage that may be undefined.
  1276.  
  1277. LCLint does not report an error when a pointer to allocated but undefined
  1278. storage is passed as an out parameter. Within the body of a function, LCLint
  1279. will assume an out parameter is allocated but not necessarily bound to a
  1280. value, so an error is reported if its value is used before it is defined.
  1281.  
  1282. LCLint reports an error if storage reachable by the caller after the call is
  1283. not defined when the function returns. This can be suppressed by
  1284. -mustdefine. When checking a call, an actual parameter corresponding to an
  1285. out parameter is assumed to be completely defined after the call returns.
  1286.  
  1287. When checking unannotated programs, many spurious use before definition
  1288. errors may be reported If impouts is on, no error is reported when an
  1289. incompletely-defined parameter is passed to a formal parameter with no
  1290. definition annotation, and the actual parameter is assumed to be defined
  1291. after the call. The /*@in@*/ annotation can be used to denote a parameter
  1292. that must be completely defined, even if impouts is on. If impouts is off,
  1293. there is an implicit in annotation on every parameter with no definition
  1294. annotation.
  1295.  
  1296. Figure 13. Use before definition.
  1297.  
  1298. 7.1.2 Relaxing Checking
  1299.  
  1300. The reldef annotation relaxes definition checking for a particular
  1301. declaration. Storage declared with a reldef annotation is assumed to be
  1302. defined when it is used, but no error is reported if it is not defined
  1303. before it is returned or passed as a parameter.
  1304.  
  1305. It is up to the programmer to check reldef fields are used correctly. They
  1306. should be avoided in most cases, but may be useful for fields of structures
  1307. that may or may not be defined depending on other constraints.
  1308.  
  1309. 7.1.3 Partially Defined Structures
  1310.  
  1311. The partial annotated can be used to relax checking of structure fields. A
  1312. structure with undefined fields may be passed as a partial parameter or
  1313. returned as a partial result. Inside a function body, no error is reported
  1314. when the field of a partial structure is used. After a call, all fields of a
  1315. structure that is passed as a partial parameter are assumed to be completely
  1316. defined.
  1317.  
  1318. 7.1.4 Global Variables
  1319.  
  1320. Special annotations can be used in the globals list of a function
  1321. declaration (Section 4.2) to describe the states of global variables before
  1322. and after the call.
  1323.  
  1324. If a global is preceded by undef, it is assumed to be undefined before the
  1325. call. Thus, no error is reported if the global is not defined when the
  1326. function is called, but an error is reported if the global is used in the
  1327. function body before it is defined.
  1328.  
  1329. The killed annotation denotes a global variable that may be undefined when
  1330. the call returns. For globals that contain dynamically allocated storage, a
  1331. killed global variable is similar to an only parameter (Section 5.2). An
  1332. error is reported if it contains the only reference to storage that is not
  1333. released before the call returns.
  1334.  
  1335. Figure 14. Annotated globals lists.
  1336.  
  1337. 7.2 Null Pointers
  1338.  
  1339. A common cause of program failures is when a null pointer is dereferenced.
  1340. LCLint detects these errors by distinguishing possibly NULL pointers at
  1341. interface boundaries.
  1342.  
  1343. The null annotation is used to indicate that a pointer value may be NULL. A
  1344. pointer declared with no null annotation, may not be NULL. If null checking
  1345. is turned on (controlled by null), LCLint will report an error when a
  1346. possibly null pointer is passed as a parameter, returned as a result, or
  1347. assigned to an external reference with no null qualifier.
  1348.  
  1349. If a pointer is declared with the null annotation, the code must check that
  1350. it is not NULL on all paths leading to the a dereference of the pointer (or
  1351. the pointer being returned or passed as a value with no null annotation).
  1352. Dereferences of possibly null pointers may be protected by conditional
  1353. statements or assertions (to see how assert is declared see Section 7.3)
  1354. that check the pointer is not NULL.
  1355.  
  1356. Consider two implementations of firstChar in Figure 15. For firstChar1,
  1357. LCLint reports an error since the pointer that is dereferenced is declared
  1358. with a null annotation. For firstChar2, no error is reported since the true
  1359. branch of the s == NULL if statement returns, so the dereference of s is
  1360. only reached if s is not NULL.
  1361.  
  1362. 7.2.1 Predicate Functions
  1363.  
  1364. Another way to protect null dereference, is to declare a function using
  1365. falsenull or truenull and call the function in a conditional statement
  1366. before the null-annotated pointer is dereferenced. The falsenull and
  1367. truenull annotations may only be used on return values for functions that
  1368. return a boolean[19] result and whose first argument is a possibly null
  1369. pointer.
  1370.  
  1371. A function is annotated with truenull is assumed to return TRUE if its first
  1372. parameter is NULL and FALSE otherwise. For example, if isNull is declared
  1373. as,
  1374.  
  1375.    /*@truenull@*/ bool isNull (/*@null@*/ char *x);
  1376.  
  1377. we could write firstChar2:
  1378.  
  1379.    char firstChar2 (/*@null@*/ char *s)
  1380.    {
  1381.       if (isNull (s)) return '\0';
  1382.       return *s;
  1383.    }
  1384.  
  1385. No error is reported since the dereference of s is only reached if isNull(s)
  1386. is false, and since isNull is declared with the truenull annotation this
  1387. means s must not be null.
  1388.  
  1389. The falsenull annotation is not quite the opposite of truenull. If a
  1390. function declared with falsenull returns TRUE, it means its parameter is not
  1391. NULL. If it returns FALSE, the parameter may or may not be NULL.
  1392.  
  1393. For example, we could define isNonEmpty to return TRUE if its parameter is
  1394. not NULL and has least one character before the NUL terminator:
  1395.  
  1396.    /*@falsenull@*/ bool isNonEmpty (/*@null@*/ char *x)
  1397.    {
  1398.      return (x != NULL && *x != `\0');
  1399.    }
  1400.  
  1401. LCLint does not check that the implementation of a function declared with
  1402. falsenull or truenull is consistent with its annotation, but assumes the
  1403. annotation is correct when code that calls the function is checked.
  1404.  
  1405. 7.2.2 Overriding Null Types
  1406.  
  1407. The null annotation may be used in a type definition to indicate that all
  1408. instances of the type may be NULL. For declarations of a type declared using
  1409. null, the null annotation in the type definition may be overridden with
  1410. notnull. This is particularly useful for parameters to hidden static
  1411. operations of abstract types where the null test has already been done
  1412. before the function is called, or function results of the type which are
  1413. never NULL. For an abstract type, notnull may not be used for parameters to
  1414. external functions, since clients should not be aware of when the concrete
  1415. representation may by NULL. Parameters to static functions in the
  1416. implementation module, however, may be declared using notnull, since they
  1417. may only be called from places where the representation is accessible.
  1418. Return values for static or external functions may be declared using
  1419. notnull.
  1420.  
  1421. Figure 16. Using notnull.
  1422.  
  1423. 7.2.3 Relaxing Null Checking
  1424.  
  1425. An additional annotation, relnull may be used to relax null checking
  1426. (relnull is analogous to reldef for definition checking). No error is
  1427. reported when a relnull value is dereferenced, or when a possibly null value
  1428. is assigned to an identifier declared using relnull.
  1429.  
  1430. This is generally used for structure fields that may or may not be null
  1431. depending on some other constraint. LCLint does not report and error when
  1432. NULL is assigned to a relnull reference, or when a relnull reference is
  1433. dereferenced. It is up to the programmer to ensure that this constraint is
  1434. satisfied before the pointer is dereferenced.
  1435.  
  1436. 7.3 Execution
  1437.  
  1438. To detect certain errors and avoid spurious errors, it is important to know
  1439. something about the control flow behavior of called functions. Without
  1440. additional information, LCLint assumes that all functions eventually return
  1441. and execution continues normally at the call site.
  1442.  
  1443. The exits annotation is used to denote a function that never returns. For
  1444. example,
  1445.  
  1446. extern /*@exits@*/ void fatalerror (/*@observer@*/ char *s);
  1447.  
  1448. declares fatalerror to never return. This allows LCLint to correctly analyze
  1449. code like,
  1450.  
  1451.    if (x == NULL) fatalerror ("Yikes!");
  1452.    *x = 3;
  1453.  
  1454. Other functions may exit, but sometimes (or usually) return normally. The
  1455. mayexit annotation denotes a function that may or may not return. This
  1456. doesn't help checking much, since LCLint must assume that a function
  1457. declared with mayexit returns normally when checking the code.
  1458.  
  1459. To be more precise, the trueexit and falseexit annotations may be used
  1460. Similar to truenull and falsenull (see Section 7.2.1), trueexit and
  1461. falseexit mean that a function always exits if the value of its first
  1462. argument is TRUE or FALSE respectively. They may be used only on functions
  1463. whose first argument has a boolean type.
  1464.  
  1465. A function declared with trueexit must exit if the value of its argument is
  1466. TRUE, and a function declared with falseexit must exit if the value of its
  1467. argument is FALSE. For example, the standard library declares assert as[20]:
  1468.  
  1469. /*@falseexit@*/ void assert (/*@sef@*/ bool /*@alt int@*/ pred);
  1470.  
  1471. This way, code like,
  1472.  
  1473.    assert (x != NULL);
  1474.  
  1475.    *x = 3;
  1476.  
  1477. is checked correctly, since the falseexit annotation on assert means the
  1478. deference of x is not reached is x != NULL is false.
  1479.  
  1480. 7.4 Special Clauses
  1481.  
  1482. Sometimes it is necessary to specify function interfaces at a lower level
  1483. than is possible with the standard annotations. For example, if a function
  1484. defines some fields of a returned structure but does not define all the
  1485. fields. The /*@special@*/ annotation is used to mark a parameter, global
  1486. variable, or return value that is described using special clauses. The usual
  1487. implicit definition rules do not apply to a special declaration.
  1488.  
  1489. Special clauses may be used to constrain the state of a parameter or return
  1490. value before or after a call. One or more special clauses may appear in a
  1491. function declaration, before the modifies or globals clauses. Special
  1492. clauses may be listed in any order, but the same special clause should not
  1493. be used more than once. Parameters used in special clauses must be annotated
  1494. with /*@special@*/ in the function header. In a special clause list, result
  1495. is used to refer to the return value of the function. If result appears in a
  1496. special clause, the function return value must be annotated with
  1497. /*@special@*/.
  1498.  
  1499. The following special clauses are used to describe the definition state or
  1500. parameters before and after the function is called and the return value
  1501. after the function returns:
  1502.  
  1503. /*@uses references@*/
  1504.  
  1505.      References in the uses clause must be completely defined before
  1506.      the function is called. They are assumed to be defined at function
  1507.      entrance when the function is checked.
  1508.  
  1509. /*@sets references@*/
  1510.  
  1511.      References in the sets clause must be allocated before the
  1512.      function is called. They are completely defined after the function
  1513.      returns. When the function is checked, they are assumed to be
  1514.      allocated at function entrance and an error is reported if there
  1515.      is a path on which they are not defined before the function
  1516.      returns.
  1517.  
  1518. /*@defines references@*/
  1519.  
  1520.      References in the defines clause must not refer to unshared,
  1521.      allocated storage before the function is called. They are
  1522.      completely defined after the function returns. When the function
  1523.      is checked, they are assumed to be undefined at function entrance
  1524.      and an error is reported if there is a path on which they are not
  1525.      defined before the function returns.
  1526.  
  1527. /*@allocates references@*/
  1528.  
  1529.      References in the allocates clause must not refer to unshared,
  1530.      allocated storage before the function is called. They are
  1531.      allocated but not necessarily defined after the function returns.
  1532.      When the function is checked, they are assumed to be undefined at
  1533.      function entrance and an error is reported if there is a path on
  1534.      which they are not allocated before the function returns.
  1535.  
  1536. /*@releases references@*/
  1537.  
  1538.      References in the releases clause are deallocated by the function.
  1539.      They must correspond to storage which could be passed as an only
  1540.      parameter before the function is called, and are dead pointers
  1541.      after the function returns. When the function is checked, they are
  1542.      assumed to be allocated at function entrance and an error is
  1543.      reported if they refer to live, allocated storage at any return
  1544.      point.
  1545.  
  1546. Additional generic special clauses can be used to describe other aspects of
  1547. the state of inner storage before or after a call. Generic special clauses
  1548. have the form state:constraint. The state is either pre (before the function
  1549. is called), or post (after the function is called). The constraint is
  1550. similar to an annotation. The following constraints are supported:
  1551.  
  1552.      Aliasing Annotations
  1553.  
  1554.      pre:only, post:only
  1555.      pre:shared, post:shared
  1556.      pre:owned, post:owned
  1557.      pre:dependent, post:dependent
  1558.  
  1559.           References refer to only, shared, owned or dependent
  1560.           storage before (pre) or after (post) the call.
  1561.  
  1562.      Exposure Annotations
  1563.  
  1564.      pre:observer, post:observer
  1565.      pre:exposed, post:exposed
  1566.  
  1567.           References refer to observer or exposed storage before
  1568.           (pre) or after (post) the call.
  1569.  
  1570.      Null State Annotations
  1571.  
  1572.      pre:isnull, post:isnull
  1573.  
  1574.           References have the value NULL before (pre) or after
  1575.           (post) the call. Note, this is not the same name or
  1576.           meaning as the null annotation (which means the value
  1577.           may be NULL.)
  1578.  
  1579.      pre:notnull, post:notnull
  1580.  
  1581.           References do not have the value NULL before (pre) or
  1582.           after (post) the call.
  1583.  
  1584. Some examples of special clauses are shown in Figure 17. The defines clause
  1585. for record_new indicates that the id field of the structure pointed to by
  1586. the result is defined, but the name field is not. So, record_create needs to
  1587. call record_setName to define the name field. Similarly, the releases clause
  1588. for record_clearName indicates that no storage is associated with the name
  1589. field of its parameter after the return, so no failure to deallocate storage
  1590. message is produced for the call to free in record_free.
  1591.  
  1592. 8. Macros
  1593.  
  1594. Macros are commonly used in C programs to implement constants or to mimic
  1595. functions without the overhead of a function call. Macros that are used to
  1596. implement functions are a persistent source of bugs in C programs, since
  1597. they may not behave like the intended function when they are invoked with
  1598. certain parameters or used in certain syntactic contexts.
  1599.  
  1600. LCLint eliminates most of the potential problems by detecting macros with
  1601. dangerous implementations and dangerous macro invocations. Whether or not a
  1602. macro definition is checked or expanded normally depends on flag settings
  1603. and control comments (see Section 8.3). Stylized macros can also be used to
  1604. define control structures for iterating through many values (see Section
  1605. 8.4).
  1606.  
  1607. 8.1 Constant Macros
  1608.  
  1609. Macros may be used to implement constants. To get type-checking for constant
  1610. macros, use the constant syntactic comment:
  1611.  
  1612. /*@constant null char *mstring_undefined@*/
  1613.  
  1614. Declared constants are not expanded and are checked according to the
  1615. declaration. A constant with a null annotation may be used as only storage.
  1616.  
  1617. 8.2 Function-like Macros
  1618.  
  1619. Using macros to imitate functions is notoriously dangerous. Consider this
  1620. broken macro for squaring a number:
  1621.  
  1622. # define square(x) x * x
  1623.  
  1624. This works fine for a simple invocation like square(i). It behaves
  1625. unexpectedly, though, if it is invoked with a parameter that has a side
  1626. effect.
  1627.  
  1628. For example, square(i++) expands to i++ * i++. Not only does this give the
  1629. incorrect result, it has undefined behavior since the order in which the
  1630. operands are evaluated is not defined. (See Section 10.1 for more
  1631. information on how expressions exhibiting undefined evaluation order
  1632. behavior are detected by LCLint.) To correct the problem we either need to
  1633. rewrite the macro so that its parameter is evaluated exactly once, or
  1634. prevent clients from invoking the macro with a parameter that has a
  1635. side-effect.
  1636.  
  1637. Another possible problem with macros is that they may produce unexpected
  1638. results because of operator precedence rules. The invocation, square(i+1)
  1639. expands to i+1*i+1, which evaluates to i+i+1 instead of the square of i+1.
  1640. To ensure the expected behavior, the macro parameter should be enclosed in
  1641. parentheses where it is used in the macro body.
  1642.  
  1643. Macros may also behave unexpectedly if they are not syntactically equivalent
  1644. to an expression. Consider the macro definition,
  1645.  
  1646. # define incCounts()  ntotal++; ncurrent++;
  1647.  
  1648. This works fine, unless it is used as a statement. For example,
  1649.  
  1650. if (x < 3) incCounts();
  1651.  
  1652. increments ntotal if x < 3 but always increments ncurrent.
  1653.  
  1654. One solution is to use the comma operator to define the macro:
  1655.  
  1656. # define incCounts()  (ntotal++, ncurrent++)
  1657.  
  1658. More complicated macros can be written using a do Ŷ while construction:
  1659.  
  1660.   # define incCounts() \
  1661.      do { ntotal++; ncurrent++; } while (FALSE)
  1662.  
  1663. LCLint detects these pitfalls in macro definitions, and checks that a macro
  1664. behaves as much like a function as possible. A client should only be able to
  1665. tell that a function was implemented by a macro if it attempts to use the
  1666. macro as a pointer to a function.
  1667.  
  1668. These checks are done by LCLint on a macro definition corresponding to a
  1669. function:
  1670.  
  1671.    * Each parameter to a macro (except those declared to be side-effect
  1672.      free, see Section 8.2.1) must be used exactly once in all possible
  1673.      executions of the macro, so side-effecting arguments behave as
  1674.      expected.[21] (Controlled by macroparams.)
  1675.    * A parameter to a macro may not be used as the left hand side of an
  1676.      assignment expression or as the operand of an increment or decrement
  1677.      operator in the macro text, since this produces non-functional
  1678.      behavior. (Controlled by macroassign.)
  1679.    * Macro parameters must be enclosed in parentheses when they are used in
  1680.      potentially dangerous contexts. (Controlled by macroparens.)
  1681.    * A macro definition must be syntactically equivalent to a statement when
  1682.      it is invoked followed by a semicolon. (Controlled by macrostmt.)
  1683.    * The type of the macro body must match the return type of the
  1684.      corresponding function. If the macro is declared with type void, its
  1685.      body may have any type but the macro value may not be used.
  1686.    * All variables declared in the body of a macro definition must be in the
  1687.      macro variable namespace, so they do not conflict with variables in the
  1688.      scope where the macro is invoked (which may be used in the macro
  1689.      parameters). By default, the macro namespace is all names prefixed by
  1690.      m_. (See Section 9.2 for information on controlling namespaces.)
  1691.  
  1692. At the call site, a macro is checked like any other function call.
  1693.  
  1694. 8.2.1 Side-Effect Free Parameters
  1695.  
  1696. Suppose we really do want to implement square as a macro, but want do so in
  1697. a safe way. One way to do this is to require that it is never invoked with a
  1698. parameter that has a side-effect. LCLint will check that this constraint
  1699. holds, if the parameter is annotated to be side-effect free. That is, the
  1700. expression corresponding to this parameter must not modify any state, so it
  1701. does not matter how many times it is evaluated. The sef annotation is used
  1702. to denote a parameter that may not have any side-effects:
  1703.  
  1704.    extern int square (/*@sef@*/ int x);
  1705.    # define square(x) ((x) *(x))
  1706.  
  1707. Now, LCLint will not report an error checking the definition of square even
  1708. though x is used more than once.
  1709.  
  1710. A message will be reported, however, if square is invoked with a parameter
  1711. that has a side-effect.
  1712.  
  1713. For the code fragment,
  1714.  
  1715. square (i++)
  1716.  
  1717. LCLint produces the message:
  1718.  
  1719.    Parameter 1 to square is declared sef, but the argument may modify i: i++
  1720.  
  1721. It is also an error to pass a non-sef macro parameter as a sef macro
  1722. parameter in the body of a macro definition. For example,
  1723.  
  1724.    extern int sumsquares (int x, int y);
  1725.    # define sumsquares(x,y) (square(x) + square(y))
  1726.  
  1727. Although x only appears once in the definition of sumsquares it will be
  1728. evaluated twice since square is expanded. LCLint reports an error when a
  1729. non-sef macro parameter is passed as a sef parameter.
  1730.  
  1731. A parameter may be passed as a sef parameter without an error being
  1732. reported, if LCLint can determine that evaluating the parameter has no
  1733. side-effects. For function calls, the modifies clause is used to determine
  1734. if a side-effect is possible.[22] To prevent many spurious errors, if the
  1735. called function has no modifies clause, LCLint will report an error only if
  1736. sefuncon is on. Justifiably paranoid programmers will insist on setting
  1737. sefuncon on, and will add modifies clauses to unconstrained functions that
  1738. are used in sef macro arguments.
  1739.  
  1740. 8.2.2 Polymorphism
  1741.  
  1742. One problem with our new definition of square is that while the original
  1743. macro would work for parameters of any numeric type, LCLint will now report
  1744. an error is the new version is used with a non-integer parameter.
  1745.  
  1746. We can use the /*@alt type;,+@> syntax to indicate that an alternate type
  1747. may be used. For example,
  1748.  
  1749.   extern int /*@alt float@*/ square (/*@sef@*/ int /*@alt float@*/ x);
  1750.   # define square(x) ((x) *(x))
  1751.  
  1752. declares square for both ints and floats.
  1753.  
  1754. Alternate types are also useful for declaring functions for which the return
  1755. value may be safely ignored (see Section 10.3.2).
  1756.  
  1757. 8.3 Controlling Macro Checking
  1758.  
  1759. By default, LCLint expands macros normally and checks the resulting code
  1760. after macros have been expanded. Flags and control comments may be used to
  1761. control which macros are expanded and which are checked as functions or
  1762. constants.
  1763.  
  1764. If the fcnmacros flag is on, LCLint assumes all macros defined with
  1765. parameter lists implement functions and checks them accordingly.
  1766. Parameterized macros are not expanded and are checked as functions with
  1767. unknown result and parameter types (or using the types in the prototype, if
  1768. one is given). The analogous flag for macros that define constants is
  1769. constmacros. If it is on, macros with no parameter lists are assumed to be
  1770. constants, and checked accordingly. The allmacros flag sets both fcnmacros
  1771. and constmacros. If the macrofcndecl flag is set, a message reports
  1772. parameterized macros with no corresponding function prototype. If the
  1773. macroconstdecl flag is set, a similar message reports macros with no
  1774. parameters with no corresponding constant declaration.
  1775.  
  1776. The macro checks described in the previous sections make sense only for
  1777. macros that are intended to replace functions or constants. When fcnmacros
  1778. or constmacros is on, more general macros need to be marked so they will not
  1779. be checked as functions or constants, and will be expanded normally. Macros
  1780. which are not meant to behave like functions should be preceded by the
  1781. /*@notfunction@*/ comment. For example,
  1782.  
  1783.    /*@notfunction@*/
  1784.    # define forever for(;;)
  1785.  
  1786. Macros preceded by notfunction are expanded normally before regular checking
  1787. is done. If a macro that is not syntactically equivalent to a statement
  1788. without a semi-colon (e.g., a macro which enters a new scope) is not
  1789. preceded by notfunction, parse errors may result when fcnmacros or
  1790. constmacros is on.
  1791.  
  1792. 8.4 Iterators
  1793.  
  1794. It is often useful to be able to execute the same code for many different
  1795. values. For example, we may want to sum all elements in an intSet that
  1796. represents a set of integers. If intSet is an abstract type, there is no
  1797. easy way of doing this in a client module without depending on the concrete
  1798. representation of the type. Instead, we could provide such a mechanism as
  1799. part of the type's implementation. We call a mechanism for looping through
  1800. many values an iterator.
  1801.  
  1802. The C language provides no mechanism for creating user-defined iterators.
  1803. LCLint supports a stylized form of iterators declared using syntactic
  1804. comments and defined using macros.
  1805.  
  1806. Iterator declarations are similar to function declarations except instead of
  1807. returning a value, they assign values to their yield parameters in each
  1808. iteration. For example, we could add this iterator declaration to intSet.h:
  1809.  
  1810. /*@iter intSet_elements (intSet s, yield int el);@*/
  1811.  
  1812. The yield annotation means that the variable passed as the second actual
  1813. argument is declared as a local variable of type int and assigned a value in
  1814. each loop iteration.
  1815.  
  1816. Defining Iterators
  1817.  
  1818. An iterator is defined using a macro. Here's one (not particularly
  1819. efficient) way of defining intSet_elements:
  1820.  
  1821.    typedef /*@abstract@*/ struct {
  1822.       int nelements;
  1823.       int *elements;
  1824.    } intSet;
  1825.    ...
  1826.    # define intSet_elements(s,m_el) \
  1827.      { int m_i; \
  1828.        for (m_i = (0); m_i <= ((s)->nelements); m_i++) { \
  1829.            int m_el = (s)->elements[(m_i)];
  1830.  
  1831.    # define end_intSet_elements }}
  1832.  
  1833. Each time through the loop, the yield parameter m_el is assigned to the next
  1834. value. After all values have been assigned to m_el for one iteration, the
  1835. loop terminates. Variables declared by the iterator macro (including the
  1836. yield parameter) are preceded by the macro variable namespace prefix m_ (see
  1837. Section 8.2) to avoid conflicts with variables defined in the scope where
  1838. the iterator is used.
  1839.  
  1840. Using Iterators
  1841.  
  1842. The general structure for using an iterator is,
  1843.  
  1844. iter (<params>) stmt; end_iter
  1845.  
  1846. For example, a client could use intSet_elements to sum the elements of an
  1847. intSet:
  1848.  
  1849.    intSet s;
  1850.    int sum = 0;
  1851.    ...
  1852.    intSet_elements (s, el) {
  1853.       sum += el;
  1854.    } end_intSet_elements;
  1855.  
  1856. The actual parameter corresponding to a yield parameter, el, is not declared
  1857. in the function scope. Instead, it is declared by the iterator and assigned
  1858. to an appropriate value for each iteration.
  1859.  
  1860. LCLint will do the following checks for uses of stylized iterators:
  1861.  
  1862.    * An invocation of the iterator iter must be balanced by a corresponding
  1863.      end, named end_iter.
  1864.    * All actual parameters must be defined, except those corresponding to
  1865.      yield parameters.
  1866.    * Yield parameters must be new identifiers, not declared in the current
  1867.      scope or any enclosing scope.
  1868.  
  1869. Iterators are a bit awkward to implement, but they enable compact, easily
  1870. understood client code. For abstract collection types, an iterator can be
  1871. used to enable clients to operate on elements of the collection without
  1872. breaking data abstraction.
  1873.  
  1874. 9. Naming Conventions
  1875.  
  1876. Naming conventions tend to be a religious issue. Generally, it doesn't
  1877. matter too much what naming convention is followed as long as one is chosen
  1878. and followed religiously. There are two kinds of naming conventions
  1879. supported by LCLint. Type-based naming conventions (Section 9.1) constrain
  1880. identifier names according to the abstract types that are accessible where
  1881. the identifier is defined. Prefix naming conventions (Section 9.2) constrain
  1882. the initial characters of identifier names according to what is being
  1883. declared and its scope. Naming conventions may be combined or different
  1884. conventions may be selected for different kinds of identifiers. In addition,
  1885. LCLint supports checking that names do not conflict with names reserved for
  1886. the standard library or implementation (Section 9.3) and that names are
  1887. sufficiently distinguishable from other names.
  1888.  
  1889. 9.1 Type-Based Naming Conventions
  1890.  
  1891. Generic naming conventions constrain valid names of identifiers. By limiting
  1892. valid names, namespaces may be preserved and programs may be more easily
  1893. understood since the name gives clues as to how and where the name is
  1894. defined and how it should be used.
  1895.  
  1896. Names may be constrained by the scope of the name (external, file static,
  1897. internal), the file in which the identifier is defined, the type of the
  1898. identifier, and global constraints.
  1899.  
  1900. 9.1.1 Czech Names
  1901.  
  1902.      Of course, this is a complete jumble to the uninitiated, and
  1903.      that's the joke.
  1904.      - Charles Simonyi, on the Hungarian naming convention
  1905.  
  1906. Czech[23] names denote operations and variables of abstract types by
  1907. preceding the names by <type>_. The remainder of the name should begin with
  1908. a lowercase character, but may use any other character besides the
  1909. underscore. Types may be named using any non-underscore characters.
  1910.  
  1911. The Czech naming convention is selected by the czech flag. If accessczech is
  1912. on, a function, variable, constant or iterator named <type>_<name> has
  1913. access to the abstract type <type>.
  1914.  
  1915. Reporting of violations of the Czech naming convention is controlled by
  1916. different flags depending on what is being declared:
  1917.  
  1918. czechfcns
  1919.  
  1920.      Functions and iterators. An error is reported for a function name
  1921.      of the form <prefix>_<name> where <prefix> is not the name of an
  1922.      accessible type. Note that if accessczech is on, a type named
  1923.      <prefix> would be accessible in a function beginning with
  1924.      <prefix>_. If accessczech is off, an error is reported instead. An
  1925.      error is reported for a function name that does not have an
  1926.      underscore if any abstract types are accessible where the function
  1927.      is defined.
  1928.  
  1929. czechvars
  1930. czechconstants
  1931. czechmacros
  1932.  
  1933.      Variables, constants and expanded macros. An error is reported if
  1934.      the identifier name starts with <prefix>_ and prefix is not the
  1935.      name of an accessible abstract type, or if an abstract type is
  1936.      accessible and the identifier name does not begin with <type>_
  1937.      where type is the name of an accessible abstract type. If
  1938.      accessczech is on, the representation of the type is visible in
  1939.      the constant or variable definition.
  1940.  
  1941. czechtypes
  1942.  
  1943.      User-defined types. An error is reported if a type name includes
  1944.      an underscore character.
  1945.  
  1946. 9.1.2 Slovak Names
  1947.  
  1948. Slovak names are similar to Czech names, except they are spelled
  1949. differently. A Slovak name is of the form <type><Name>. The type prefix may
  1950. not use uppercase characters. The remainder of the name starts with the
  1951. first uppercase character.
  1952.  
  1953. The slovak flag selects the Slovak naming convention. Like Czech names, it
  1954. may be used with accessslovak to control access to abstract representations.
  1955. The slovakfcns, slovakvars, slovakconstants, and slovakmacros flags are
  1956. analogous to the similar Czech flags. If slovaktype is on, an error is
  1957. reported if a type name includes an uppercase letter.
  1958.  
  1959. 9.1.3 Czechoslovak Names
  1960.  
  1961. Czechoslovak names are a combination of Czech names and Slovak names.
  1962. Operations may be named either <type>_ followed by any sequence of
  1963. non-underscore characters, or <type> followed by an uppercase letter and any
  1964. sequence of characters. Czechoslovak names have been out of favor since
  1965. 1993, but may be necessary for checking legacy code. The czechoslovakfcns,
  1966. czechoslovakvars, czechoslovakmacros, and czechoslovakconstants flags are
  1967. analogous to the similar Czech flags. If czechoslovaktype is on, an error is
  1968. reported if a type name contains either an uppercase letter or an underscore
  1969. character.
  1970.  
  1971. 9.2 Namespace Prefixes
  1972.  
  1973. Another way to restrict names is to constrain the leading character
  1974. sequences of various kinds of identifiers. For example, a the names of all
  1975. user-defined types might begin with "T" followed by an uppercase letter and
  1976. all file static names begin with an uppercase letter. This may be useful for
  1977. enforcing a namespace (e.g., all names exported by the X-windows library
  1978. should begin with "X") or just making programs easier to understand by
  1979. establishing an enforced convention. LCLint can be used to constrain
  1980. identifiers in this way to detect identifiers inconsistent with prefixes.
  1981.  
  1982. All namespace flags are of the form, -<context>prefix <string>. For example,
  1983. the macro variable namespace restricting identifiers declared in macro
  1984. bodies to be preceded by "m_" would be selected by -macrovarprefix "m_". The
  1985. string may contain regular characters that may appear in a C identifier.
  1986. These must match the initial characters of the identifier name. In addition,
  1987. special characters (shown in Table 1) can be used to denoted a class of
  1988. characters.[24] The * character may be used at the end of a prefix string to
  1989. specify the rest of the identifier is zero or more characters matching the
  1990. character immediately before the *. For example, the prefix string "T&*"
  1991. matches "T" or "TWINDOW" but not "Twin".
  1992.  
  1993. ^    Any uppercase letter, A-Z
  1994. &    Any lowercase letter, a-z
  1995. %    Any character that is not an uppercase letter (allows lowercase
  1996.      letters, digits and underscore)
  1997. ~    Any character that is not a lowercase letter (allows uppercase letters,
  1998.      digits and underscore)
  1999. $    Any letter (a-z, A-Z)
  2000. /    Any letter or digit (A-Z, a-z, 0-9)
  2001. ?    Any character valid in a C identifier
  2002. #    Any digit, 0-9
  2003.  
  2004. Table 1. Prefix character codes.
  2005.  
  2006. Different prefixes can be selected for the following identifier contexts:
  2007.  
  2008. macrovarprefix
  2009.  
  2010.      Any variable declared inside a macro body
  2011.  
  2012. uncheckedmacroprefix
  2013.  
  2014.      Any macro that is not checked as a function or constant (see
  2015.      Section 8.4)
  2016.  
  2017. tagprefix
  2018.  
  2019.      Tags for struct, union and enum declarations
  2020.  
  2021. enumprefix
  2022.  
  2023.      Members of enum types
  2024.  
  2025. typeprefix
  2026.  
  2027.      Name of a user-defined type
  2028.  
  2029. filestaticprefix
  2030.  
  2031.      Any identifier with file static scope
  2032.  
  2033. globvarprefix
  2034.  
  2035.      Any variable (not of function type) with global variables scope
  2036.  
  2037. externalprefix
  2038.  
  2039.      Any exported identifier
  2040.  
  2041. If an identifier is in more than one of the namespace contexts, the most
  2042. specific defined namespace prefix is used (e.g., a global variable is also
  2043. an exported identifier, so if globalvarprefix is set, it is checked against
  2044. the variable name; if not, the identifier is checked against the
  2045. externalprefix.)
  2046.  
  2047. For each prefix flag, a corresponding flag named <prefixname>exclude
  2048. controls whether errors are reported if identifiers in a different namespace
  2049. match the namespace prefix. For example, if macrovarprefixexclude is on,
  2050. LCLint checks that no identifier that is not a variable declared inside a
  2051. macro body uses the macro variable prefix.
  2052.  
  2053. Here is a (somewhat draconian) sample naming convention:
  2054.  
  2055. -uncheckedmacroprefix "~*"
  2056. unchecked macros have no lowercase letters
  2057. -typeprefix "T^&*"
  2058. all type typenames begin with T followed by an uppercase letter. The rest of
  2059. the name is all lowercase letters.
  2060. +typeprefixexclude
  2061. no identifier that does not name a user-defined type may begin with the type
  2062. name prefix (set above)
  2063. -filestaticprefix"^&&&"
  2064. file static scope variables begin with an uppercase letter and three
  2065. lowercase letters
  2066. -globvarprefix "G"
  2067. all global variables variables start with G
  2068. +globvarprefixexclude
  2069. no identifier that is not a global variable starts with G
  2070.  
  2071. 9.3 Naming Restrictions
  2072.  
  2073. Additional naming restrictions can be used to check that names do no
  2074. conflict with names reserved for the standard library, and that identifier
  2075. are sufficiently distinct (either for the compiler and linker, or for the
  2076. programmer.) Restrictions may be different for names that are needed by the
  2077. linker (external names) and names that are only needed during compilations
  2078. (internal names). Names of non-static functions and global variables are
  2079. external; all other names are internal.
  2080.  
  2081. 9.3.1 Reserved Names
  2082.  
  2083. Many names are reserved for the implementation and standard library. A
  2084. complete list of reserved names can be found in [vdL, p. 126-128] or [ANSI,
  2085. Section 4]. Some name prefixes such as str followed by a lowercase character
  2086. are reserved for future library extensions. Most C compilers do not detect
  2087. naming conflicts, and they can lead to unpredictable program behavior. If
  2088. ansireserved is on, LCLint reports errors for external names that conflict
  2089. with reserved names. If ansireservedinternal is on, errors are also reported
  2090. for internal names.
  2091.  
  2092. 9.3.2 Distinct Identifiers
  2093.  
  2094.      The decision to retain the old six-character case-insensitive
  2095.      restriction on significance was most painful.
  2096.      - ANSI C Rationale
  2097.  
  2098. LCLint can check that identifiers differ within a given number of
  2099. characters, optionally ignoring alphabetic case and differences between
  2100. characters that look similar. The number of significant characters may be
  2101. different for external and internal names.
  2102.  
  2103. Using +distinctexternalnames sets the number of significant characters for
  2104. external names to six and makes alphabetical case insignificant for external
  2105. names. This is the minimum significance acceptable in an ANSI-conforming
  2106. compiler. Most modern compilers exceed these minimums (which are
  2107. particularly hard to follow if one uses the Czech or Slovak naming
  2108. convention). The number of significant characters can be changed using the
  2109. externalnamelength <number> flag. If externalnamecaseinsensitive is on,
  2110. alphabetical case is ignored in comparing external names. LCLint reports
  2111. identifiers that differ only in alphabetic case.
  2112.  
  2113. For internal identifiers, a conforming compiler must recognize at least 31
  2114. characters and treat alphabetical cases distinctly. Nevertheless, it may
  2115. still be useful to check that internal names are more distinct then required
  2116. by the compiler to minimize the likelihood that identifiers are confused in
  2117. the program. Analogously to external names, the internalnamelength <number>
  2118. flag sets the number of significant characters in an internal name and
  2119. internalnamecaseinsensitive sets the case sensitivity. The
  2120. internalnamelookalike flag further restricts distinctions between
  2121. identifiers. When set, similar-looking characters match -- the lowercase
  2122. letter "l" matches the uppercase letter "I" and the number "1"; the letter
  2123. "O" or "o" matches the number "0"; "5" matches "S"; and "2" matches "Z".
  2124. Identifiers that are not distinct except for look-alike characters will
  2125. produce an error message. External names are also internal names, so they
  2126. must satisfy both the external and internal distinct identifier checks.
  2127.  
  2128. Figure 18 illustrates some of the name checking done by LCLint.
  2129.  
  2130. 10. Other Checks
  2131.  
  2132. The section describes other errors detected by LCLint that are not directly
  2133. related to extra information provided in annotations. Many of the checks are
  2134. significantly improved, however, because of the extra information that is
  2135. known about the program.
  2136.  
  2137. 10.1 Undefined Evaluation Order
  2138.  
  2139. The order in which side effects take place in a C program is not entirely
  2140. defined by the code. Certain execution points are known as sequence points
  2141. -- a function call (after the arguments have been evaluated), the end of a
  2142. full expression (an initializer, expression in an expression statement, the
  2143. control expression of an if, switch, while or do statement, each expression
  2144. of a for statement, and the expression in a return statement), and after the
  2145. first operand or a &&, ||, ? or , operand.
  2146.  
  2147. All side effects before a sequence point must be complete before the
  2148. sequence point, and no evaluations after the sequence point shall have taken
  2149. place [ANSI, Section 2.1.2.3]. Between sequence points, side effects and
  2150. evaluations may take place in any order. Hence, the order in which
  2151. expressions or arguments are evaluated is not specified. Compilers are free
  2152. to evaluate function arguments and parts of expressions (that do not contain
  2153. sequence points) in any order. The behavior of code that uses a value that
  2154. is modified by another expression that is not required to be evaluated
  2155. before or after the other use is undefined.
  2156.  
  2157. LCLint detects instances where undetermined order of evaluation produces
  2158. undefined behavior. If modifies clauses and globals lists are used, this
  2159. checking is enabled in expressions involving function calls. Evaluation
  2160. order checking is controlled by the evalorder flag.
  2161.  
  2162. When checking systems without modifies and globals information, evaluation
  2163. order checking may report errors when unconstrained functions are called in
  2164. procedure arguments. Since LCLint has no annotations to constrain what these
  2165. functions may modify, it cannot be guaranteed that the evaluation order is
  2166. defined if another argument calls an unconstrained function or uses a global
  2167. variable or storage reachable from a parameter to the unconstrained
  2168. function. Its best to add modifies and globals clauses to constrain the
  2169. unconstrained functions in ways that eliminate the possibility of undefined
  2170. behavior. For large legacy systems, this may require too much effort.
  2171. Instead, the -evalorderuncon flag may be used to prevent reporting of
  2172. undefined behavior due to the order of evaluation of unconstrained
  2173. functions.
  2174.  
  2175. Figure 19. Evaluation order
  2176.  
  2177. 10.2 Problematic Control Structures
  2178.  
  2179. A number of control structures that are syntactically legal may indicate
  2180. likely bugs in programs. LCLint can detect errors involving likely infinite
  2181. loops (Section 10.2.1), fall through cases and missing cases in switch
  2182. statements (Section 10.2.2), break statements within deeply nested loops or
  2183. switches (Section 10.2.3), clauses of if, while or for statements that are
  2184. empty statements or unblocked single statements (Section 10.2.4) and
  2185. incomplete if-else logic (Section 10.2.5). Although any of these may appear
  2186. in a correct program, depending on the programming style used they may
  2187. indicate likely bugs or style violations that should be detected and
  2188. eliminated.
  2189.  
  2190. 10.2.1 Likely Infinite Loops
  2191.  
  2192. LCLint reports an error if it detects a loop that appears to be infinite. An
  2193. error is reported for a loop which does not modify any value used in its
  2194. condition test inside the body of the loop or in the condition test itself.
  2195. This checking is enhanced by modifies clauses and globals lists since they
  2196. provide more information about what global variable may be used in the
  2197. condition test and what values may be modified by function calls in the loop
  2198. body.
  2199.  
  2200. Figure 20 shows examples of infinite loops detected by LCLint. An error is
  2201. reported for the loop in line 14, since neither of the values used in the
  2202. loop condition (x directly and glob1 through the call to f) is modified by
  2203. the body of the loop. If the declaration of g is changed to include glob1 in
  2204. the modifies clause no error is reported. (In this example, if we assume the
  2205. annotations are correct, then the programmer has probably called the wrong
  2206. function in the loop body. This isn't surprising, given the horrible choices
  2207. of function and variable names!)
  2208.  
  2209. If an unconstrained function is called within the loop body, LCLint will
  2210. assume that it modifies a value used in the condition test and not report an
  2211. infinite loop error, unless infloopsuncon is on. If infloopsuncon is on,
  2212. LCLint will report infinite loop errors for loops where there is no explicit
  2213. modification of a value used in the condition test, but where they may be an
  2214. undetected modification through a call to an unconstrained function (e.g.,
  2215. line 15 in Figure 20).
  2216.  
  2217. 10.2.2 Switches
  2218.  
  2219. The automatic fall-through of C switch statements is almost never the
  2220. intended behavior.[25] LCLint detects case statements with code that may
  2221. fall through to the next case. The casebreak flag controls reporting of fall
  2222. through cases. A single fall through case may be marked by preceding the
  2223. case keyword with /*@fallthrough@*/ to indicate explicitly that execution
  2224. falls through to this case.
  2225.  
  2226. For switches on enum types, LCLint reports an error if a member of the
  2227. enumerator does not appear as a case in the switch body (and there is no
  2228. default case). (Controlled by misscase.)
  2229.  
  2230. An example of switch checking is shown in Figure 21.
  2231.  
  2232. 10.2.3 Deep Breaks
  2233.  
  2234. There is no syntax provided by C (other than goto) for breaking out of a
  2235. nested loop. All break and continue statements act only on the innermost
  2236. surrounding loop or switch. This often leads to serious problems[26] when a
  2237. programmer intends to break the outer loop or switch instead. LCLint
  2238. optionally reports errors for break and continue statements in nested
  2239. contexts.
  2240.  
  2241.    * break inside a loop (while or for) that is inside a loop. Controlled by
  2242.      looploopbreak. To indicate that a break is inside an inner loop,
  2243.      precede the break by /*@innerbreak@*/.
  2244.    * break inside a loop that is inside a switch statement. Controlled by
  2245.      switchloopbreak. To mark the break as a loop break, precede the break
  2246.      by /*@loopbreak@*/.
  2247.    * break inside a switch statement that is inside a loop. Controlled by
  2248.      loopswitchbreak. To mark the break as a switch break, precede the break
  2249.      by /*@switchbreak@*/.
  2250.    * break inside a switch inside another switch. Controlled by
  2251.      switchswitchbreak. To indicate that the break is for the inner switch,
  2252.      use /*@innerbreak@*/.
  2253.  
  2254. Since continue only makes sense within loops, errors are only reported for
  2255. continue statements within nested loops. (Controlled by looploopcontinue.) A
  2256. safe inner continue may be precede by /*@innercontinue@*/ to suppress error
  2257. messages locally. The deepbreak flag sets all nested break and continue
  2258. checking flags.
  2259.  
  2260. LCLint reports an error if the marker preceding a break is not consistent
  2261. with its effect. An error is reported if innerbreak precedes a break that is
  2262. not breaking an inner loop, switchbreak precedes a break that is not
  2263. breaking a switch, or loopbreak precedes a break that is not breaking a
  2264. loop.
  2265.  
  2266. 10.2.4 Loop and If Bodies
  2267.  
  2268. An empty statement after an if, while or for often indicates a potential
  2269. bug. A single statement (i.e., not a compound block) after an if, while or
  2270. for is not likely to indicate a bug, but make the code harder to read and
  2271. edit. LCLint can report errors for if or loop statements with empty bodies
  2272. or bodies that are not compound statements. Separate flags control checking
  2273. for statements following an if, while or for:
  2274.  
  2275.    * [if, while, for]empty -- report errors for empty bodies (e.g., if (x >
  2276.      3) ;)
  2277.    * [if, while, for]block -- report errors for non-block bodies (e.g., if
  2278.      (x > 3) x++;)
  2279.  
  2280. The if statement checks also apply to the body of the else clause. An
  2281. ifblock error is not reported if the body of the else clause is an if
  2282. statement, to allow else if chains.
  2283.  
  2284. 10.2.5 Complete if-else Logic
  2285.  
  2286. Although it may be perfectly reasonable in many contexts, an if-else chain
  2287. with no final else may indicate missing logic or forgetting to check error
  2288. cases. If elseifcomplete is on, LCLint reports errors when an if statement
  2289. that is the body of an else clause does not have a matching else clause. For
  2290. example, the code,
  2291.  
  2292.    if (x == 0) { return "nil"; }
  2293.    else if (x == 1) { return "many"; }
  2294.  
  2295. produces an error message since the second if has no matching else branch.
  2296.  
  2297. 10.3 Suspicious Statements
  2298.  
  2299. LCLint detects errors involving statements with no apparent effects (Section
  2300. 10.3.1) and statements that ignore the result of a called function (Section
  2301. 10.3.2).
  2302.  
  2303. 10.3.1 Statements with No Effects
  2304.  
  2305. LCLint can report errors for statements that have no effect. (Controlled by
  2306. noeffect.) Because of modifies clauses, LCLint can detect more errors than
  2307. traditional checkers. Unless the noeffectuncon flag is on, errors are not
  2308. reported for statements that involve calls to unconstrained functions since
  2309. the unconstrained function may cause a modification.
  2310.  
  2311. Figure 22. Statements with no effect.
  2312.  
  2313. 10.3.2 Ignored Return Values
  2314.  
  2315. LCLint reports an error when a return value is ignored. Checking may be
  2316. controlled based on the type of the return value: retvalint controls
  2317. reporting of ignored return values of type int, and retvalbool for return
  2318. values of type bool, and retvalothers for all other types. A function
  2319. statement may be cast to void to prevent this error from being reported.
  2320.  
  2321. Alternate types (Section 8.2.2) can be used to declare functions that return
  2322. values that may safely be ignored by declaring the result type to
  2323. alternately by void. Several functions in the standard library are specified
  2324. to alternately return void to prevent ignored return value errors for
  2325. standard library functions (e.g., strcpy) where the result may be safely
  2326. ignored (see Apppendix F).
  2327.  
  2328. Figure 23 shows example of ignored return value errors reported by LCLint.
  2329.  
  2330. 10.4 Unused Declarations
  2331.  
  2332. LCLint detects constants, functions, parameters, variables, types,
  2333. enumerator members, and structure or union fields that are declared but
  2334. never used. The flags constuse, fcnuse, paramuse, varuse, typeuse,
  2335. enummemuse and fielduse control whether unused declaration errors are
  2336. reported for each kind of declaration. Errors for exported declarations are
  2337. reported only if topuse is on (see Section 10.5).
  2338.  
  2339. The /*@unused@*/ annotation can be used before a declaration to indicate
  2340. that the item declared need not be used. Unused declaration errors are not
  2341. reported for identifiers declared with unused.
  2342.  
  2343. 10.5 Complete Programs
  2344.  
  2345. LCLint can be used on both complete and partial programs. When checking
  2346. complete programs, additional checks can be done to ensure that every
  2347. identifier declared by the program is defined and used, and that functions
  2348. that do not need to be exported are declared static.
  2349.  
  2350. LCLint checks that all declared variables and functions are defined
  2351. (controlled by compdef). Declarations of functions and variables that are
  2352. defined in an external library, may be preceded by /*@external@*/ to
  2353. suppress undefined declaration errors.
  2354.  
  2355. LCLint reports external declarations which are unused (Controlled by
  2356. topuse). Which declarations are reported also depends on the declaration use
  2357. flags (see Section 10.4).
  2358.  
  2359. The partial flag sets flags for checking a partial system. Top-level unused
  2360. declarations, undefined declarations, and unnecessary external names are not
  2361. reported if partial is set.
  2362.  
  2363. 10.5.1 Unnecessary External Names
  2364.  
  2365. LCLint can report variables and functions that are declared with global
  2366. scope (i.e., without using static), that are not used outside the file in
  2367. which they are defined. In a stand-alone system, these identifiers should
  2368. usually be declared using static to limit their scope. If the exportstatic
  2369. flag is on, LCLint will report declarations that could have file scope. It
  2370. should only be used when all relevant source files are listed on the LCLint
  2371. command line; otherwise, variables and functions may be incorrectly
  2372. identified as only used in the file scope since LCLint did not process the
  2373. other file in which they are used.
  2374.  
  2375. 10.5.2 Declarations Missing from Headers
  2376.  
  2377. A common practice in C programming styles, is that every function or
  2378. variable exported by M.c is declared in M.h. If the exportheader flag is on,
  2379. LCLint will report exported declarations in M.c that are not declared in
  2380. M.h.
  2381.  
  2382. 10.6 Compiler Limits
  2383.  
  2384. The ANSI Standard includes limits on minimum numbers that a conforming
  2385. compiler must support. Whether of not a particular compiler exceeds these
  2386. limits, it is worth checking that a program does not exceed them so that it
  2387. may be safely compiled by other compilers. In addition, exceeding a limit
  2388. may indicate a problem in the code (e.g., it is too complex if the control
  2389. nest depth limit is exceeded) that should be fixed regardless of the
  2390. compiler. The following limits are checked by LCLint. For each limit, the
  2391. maximum value may be set from the command line (or locally using a stylized
  2392. comment). If the ansilimits flag is on, all limits are checked with the
  2393. minimum values of a conforming compiler.
  2394.  
  2395. includenest
  2396.  
  2397.      Maximum nesting depth of file inclusion (#include). (ANSI minimum
  2398.      is 8)
  2399.  
  2400. controlnestdepth
  2401.  
  2402.      Maximum nesting of compound statements, control structures. (ANSI
  2403.      minimum is 15)
  2404.  
  2405. numenummembers
  2406.  
  2407.      Number of members in an enum declaration. (ANSI minimum is 127)
  2408.  
  2409. numstructfields
  2410.  
  2411.      Number of fields in a struct or union declaration. (ANSI minimum
  2412.      is 127)
  2413.  
  2414.      Since human beings themselves are not fully debugged yet, there
  2415.      will be bugs in your code no matter what you do.
  2416.      - Chris Mason, Zero-defects memo (Microsoft Secrets, Cusumano and
  2417.      Selby)
  2418.  
  2419. Appendix A Availability
  2420.  
  2421. The web home page for LCLint is
  2422. http://larch-www.lcs.mit.edu:8001/larch/lclint/index.html. It includes a
  2423. this guide in postscript format, samples demonstrating LCLint, and links to
  2424. related web sites.
  2425.  
  2426. LCLint can be downloaded from
  2427. http://larch-www.lcs.mit.edu:8001/larch/lclint/download.html or obtained via
  2428. anonymous ftp from ftp://larch.lcs.mit.edu/pub/Larch/lclint/
  2429.  
  2430. Several UNIX platforms are supported and source code is provided for other
  2431. platforms.
  2432.  
  2433. LCLint can also be run remotely using a form at
  2434. http://larch-www.lcs.mit.edu:8001/larch/lclint/run.html
  2435.  
  2436. Appendix B Communication
  2437.  
  2438. LCLint development is largely driven by suggestions and comments from users.
  2439. We are also very interested in hearing about your experiences using LCLint
  2440. in developing or maintaining programs, enforcing coding standards, or
  2441. teaching courses. For general information, suggestions, and questions on
  2442. LCLint send mail to lclint@larch.lcs.mit.edu.
  2443.  
  2444. To report a bug in LCLint send a message to lclint-bug@larch.lcs.mit.edu.
  2445.  
  2446. There are two mailing lists associated with LCLint:
  2447.  
  2448. lclint-announce@larch.lcs.mit.edu
  2449.  
  2450.      Reserved for announcements of new releases and bug fixes.
  2451.      (Everyone who sends mail regarding LCLint is added to this list.)
  2452.  
  2453. lclint-interest@larch.lcs.mit.edu
  2454.  
  2455.      Informal discussions on the use and development of LCLint. To
  2456.      subscribe, send a (human-readable) message to
  2457.      lclint-request@larch.lcs.mit.edu, or use a form.
  2458.  
  2459. LCLint discussions relating to checks enabled by specifications or
  2460. annotations are welcome in the comp.specification.larch usenet group.
  2461. Messages more focused on C-specific checking would be more appropriate for
  2462. the lclint-interest list of one of the C language groups.
  2463.  
  2464. Appendix C Flags
  2465.  
  2466. Flags can be grouped into four major categories:
  2467.  
  2468.    * Global flags for controlling initializations and global behavior
  2469.    * Message format flags for controlling how messages are displayed
  2470.    * Mode selectors for coarse control of LCLint checking
  2471.    * Checking flags that control checking and what classes of messages are
  2472.      reported
  2473.  
  2474. Global flags can be used in initialization files and at the command line;
  2475. all other flags may also be used in control comments.
  2476.  
  2477. Global Flags
  2478.  
  2479. Global flags can be set at the command line or in an options file, but
  2480. cannot be set locally using stylized comments. These flags control on-line
  2481. help, initialization files, pre-processor flags, libraries and output.
  2482.  
  2483. Help
  2484.  
  2485. On-line help provides documentation on LCLint operation and flags. When a
  2486. help flag is used, no checking is done by LCLint. Help flags may be preceded
  2487. by - or +.
  2488.  
  2489. help
  2490.  
  2491.      Display general help overview, including list of additional help
  2492.      topics.
  2493.  
  2494. help <topic>
  2495.  
  2496.      Display help on <topic>. Available topics:
  2497.  
  2498.            annotations     describe annotations
  2499.            comments        describe control comments
  2500.            flags           summarize flag categories
  2501.  
  2502.            flags <category>all flags pertaining to <category> (one of the
  2503.                            categories listed by lclint -help flags)
  2504.            flags alpha     all flags in alphabetical order
  2505.            flags full      print a full description of all flags
  2506.            mail            print information on mailing lists
  2507.            modes           flags settings in modes
  2508.            prefixcodes     character codes for setting namespace prefixes
  2509.  
  2510.            references      print references to relevant papers and web
  2511.                            sites
  2512.            vars            describe environment variables
  2513.            version         print maintainer and version information
  2514.  
  2515. help <flag>
  2516.  
  2517.      Describe flag <flag>. (May list several flags.)
  2518.  
  2519. warnflags
  2520.  
  2521.      Display a warning when a flag is set in a surprising way. An error
  2522.      is reported if an obsolete (LCLint Version 1.4 or earlier) flag is
  2523.      set, a flag is set to its current value (i.e., the + or - may be
  2524.      wrong), or a mode selector flag is set after mode checking flags
  2525.      that will be reset by the mode were set. By default, warnflags is
  2526.      on. To suppress flag warnings, use -warnflags.
  2527.  
  2528. Initialization
  2529.  
  2530. These flags control directories and files used by LCLint. They may be used
  2531. from the command line or in an options file, but may not be used as control
  2532. comments in the source code. Except where noted. they have the same meaning
  2533. preceded by - or +.
  2534.  
  2535. tmpdir <directory>
  2536.  
  2537.      Set directory for writing temp files. Default is /tmp/.
  2538.  
  2539. I<directory>
  2540.  
  2541.      Add directory to path searched for C include files. Note there is
  2542.      no space after the I, to be consistent with C preprocessor flags.
  2543.  
  2544. S<directory>
  2545.  
  2546.      Add directory to path search for .lcl specification files.
  2547.  
  2548. f <file>
  2549.  
  2550.      Load options file <file>. If this flag is used from the command
  2551.      line, the default ~/.lclintrc file is not loaded. This flag may be
  2552.      used in an options file to load in another options file.
  2553.  
  2554. nof
  2555.  
  2556.      Prevents the default options files (./.lclintrc and ~/.lclintrc)
  2557.      from being loaded. (Setting -nof overrides +nof, causing the
  2558.      options files to be loaded normally.)
  2559.  
  2560. systemdirs
  2561.  
  2562.      Set directories for system files (default is "/usr/include").
  2563.      Separate directories with colons (e.g.,
  2564.      "/usr/include:/usr/local/lib"). Flag settings propagate to files
  2565.      in a system directory. If -systemdirerrors is set, no errors are
  2566.      reported for files in system directories.
  2567.  
  2568. Pre-processor
  2569.  
  2570. These flags are used to define or undefine pre-processor constants. The
  2571. -I<directory> flag is also passed to the C pre-processor.
  2572.  
  2573. D<initializer>
  2574.  
  2575.      Passed to the C pre-processor.
  2576.  
  2577. U<initializer>
  2578.  
  2579.      Passed to the C pre-processor
  2580.  
  2581. Libraries
  2582.  
  2583. These flags control the creation and use of libraries.
  2584.  
  2585. dump <file>
  2586.  
  2587.      Save state in <file> for loading. The default extension .lcd is
  2588.      added if <file> has no extension.
  2589.  
  2590. load <file>
  2591.  
  2592.      Load state from <file> (created by -dump). The default extension
  2593.      .lcd is added if <file> has no extension. Only one library file
  2594.      may be loaded.
  2595.  
  2596. By default, the standard library is loaded if the -load flag is not used to
  2597. load a user library. If no user library is loaded, one of the following
  2598. flags may be used to select a different standard library. Precede the flag
  2599. by + to load the described library (or prevent a library from being loaded
  2600. using nolib).
  2601.  
  2602. nolib
  2603.  
  2604.      Do not load any library. This prevents the standard library from
  2605.      being loaded.
  2606.  
  2607. strictlib
  2608.  
  2609.      Use strict version of standard library (See Apppendix F).
  2610.  
  2611. unixlib
  2612.  
  2613.      Use UNIX version of standard library (See Apppendix F).
  2614.  
  2615. Output
  2616.  
  2617. These flags control what additional information is printed by LCLint.
  2618. Setting +<flag> causes the described information to be printed; setting
  2619. -<flag> prevents it. By default, all these flags are off.
  2620.  
  2621. showsummary
  2622.  
  2623.      Show a summary of all errors reported and suppressed. Counts of
  2624.      suppressed errors are not necessarily correct since turning a flag
  2625.      off may prevent some checking from being done to save computation,
  2626.      and errors that are not reported may propagate differently from
  2627.      when they are reported.
  2628.  
  2629. showscan
  2630.  
  2631.      Show file names are they are processed.
  2632.  
  2633. showalluses
  2634.  
  2635.      Show list of uses of all external identifiers sorted by number of
  2636.      uses.
  2637.  
  2638. stats
  2639.  
  2640.      Display number of lines processed and checking time.
  2641.  
  2642. timedist
  2643.  
  2644.      Display distribution of where checking time is spent.
  2645.  
  2646. quiet
  2647.  
  2648.      Suppress herald and error count. (If quiet is not set, LCLint
  2649.      prints out a herald with version information before checking
  2650.      begins, and a line summarizing the total number of errors
  2651.      reported.)
  2652.  
  2653. whichlib
  2654.  
  2655.      Print out the standard library filename and creation information.
  2656.  
  2657. limit <number>
  2658.  
  2659.      At most <number> similar errors are reported consecutively.
  2660.      Further errors are suppressed, and a message showing the number of
  2661.      suppressed messages is printed.
  2662.  
  2663. Expected Errors
  2664.  
  2665. Normally, LCLint will expect to report no errors. The exit status will be
  2666. success (0) if no errors are reported, and failure if any errors are
  2667. reported. Flags can be used to set the expected number of reported errors.
  2668. Because of the provided error suppression mechanisms, these options should
  2669. probably not be used for final checking real programs but may be useful in
  2670. developing programs using make.
  2671.  
  2672. expect <number>
  2673.  
  2674.      Exactly <number> code errors are expected. LCLint will exit with
  2675.      failure exit status unless <number> code errors are detected.
  2676.  
  2677. Message Format
  2678.  
  2679. These flags control how messages are printed. They may be set at the command
  2680. line, in options files, or locally in syntactic comments. The linelen and
  2681. limit flags may be preceded by + or - with the same meaning; for the other
  2682. flags, + turns on the describe printing and - turns it off. The box to the
  2683. left of each flag gives its default value.
  2684.  
  2685. showcolumn
  2686.  
  2687.      Show column number where error is found. Default: +
  2688.  
  2689. showfunc
  2690.  
  2691.      Show name of function (or macro) definition containing error. The
  2692.      function name is printed once before the first message detected in
  2693.      that function. Default: +
  2694.  
  2695. showallconjs
  2696.  
  2697.      Show all possible alternate types (see Section 8.2.2). Default: -
  2698.  
  2699. hints
  2700.  
  2701.      Provide hints describing an error and how a message may be
  2702.      suppressed for the first error reported in each error class.
  2703.      Default: +
  2704.  
  2705. forcehints
  2706.  
  2707.      Provide hints for all errors reported, even if the hint has
  2708.      already been displayed for the same error class. Default: -
  2709.  
  2710. linelen <number>
  2711.  
  2712.      Set length of maximum message line to <number> characters. LCLint
  2713.      will split messages longer than <number> characters long into
  2714.      multiple lines. Default: 80
  2715.  
  2716. Mode Selector Flags
  2717.  
  2718. Mode selects flags set the mode checking flags to predefined values. They
  2719. provide a quick coarse-grain way of controlling what classes of errors are
  2720. reported. Specific checking flags may be set after a mode flag to override
  2721. the mode settings. Mode flags may be used locally, however the mode settings
  2722. will override specific command line flag settings. A warning is produced if
  2723. a mode flag is used after a mode checking flag has been set.
  2724.  
  2725. These are brief descriptions to give a general idea of what each mode does.
  2726. To see the complete flag settings in each mode, use lclint -help modes. A
  2727. mode flag has the same effect when used with either + or -.
  2728.  
  2729. weak
  2730.  
  2731.      Weak checking, intended for typical unannotated C code. No
  2732.      modifies checking, macro checking, rep exposure, or clean
  2733.      interface checking is done. Return values of type int may be
  2734.      ignored. The types bool, int, char and user-defined enum types are
  2735.      all equivalent. Old style declarations are unreported.
  2736.  
  2737. standard
  2738.  
  2739.      The default mode. All checking done by weak, plus modifies
  2740.      checking, global alias checking, use all parameters, using
  2741.      released storage, ignored return values or any type, macro
  2742.      checking, unreachable code, infinite loops, and fall-through
  2743.      cases. The types bool, int and char are distinct. Old style
  2744.      declarations are reported.
  2745.  
  2746. checks
  2747.  
  2748.      Moderately strict checking. All checking done by standard, plus
  2749.      must modification checking, rep exposure, return alias, memory
  2750.      management and complete interfaces.
  2751.  
  2752. strict
  2753.  
  2754.      Absurdly strict checking. All checking done by checks, plus
  2755.      modifications and global variables used in unspecified functions,
  2756.      strict standard library, and strict typing of C operators. A
  2757.      special reward will be presented to the first person to produce a
  2758.      real program that produces no errors with strict checking.
  2759.  
  2760. Checking Flags
  2761.  
  2762. These flags control checking done by LCLint. They may be set locally using
  2763. syntactic comments, from the command line, or in an options file. Some flags
  2764. directly control whether a certain class of message is reported. Preceding
  2765. the flag by + turns reporting on, and preceding the flag by - turns
  2766. reporting off. Other flags control checking less directly by determining
  2767. default values (what annotations are implicit), making types equivalent (to
  2768. prevent certain type errors), controlling representation access, etc. For
  2769. these flags, the effect of + is described, and the effect of - is the
  2770. opposite (or explicitly explained if there is no clear opposite). The
  2771. organization of this section mirrors Sections 3-10.
  2772.  
  2773. Key
  2774.  
  2775. Under each flag name is a flag descriptor encoding the what kind of flag it
  2776. is and its default value. The descriptions are:
  2777.  
  2778. plain: -
  2779.  
  2780.      A plain flag. The value after the colon gives the default setting
  2781.      (e.g., this flag is off.)
  2782.  
  2783. m: --++
  2784.  
  2785.      A mode checking flag. The value of the flag is set by the mode
  2786.      selector. The four signs give the setting in the weak, standard,
  2787.      checks and strict modes. (e.g., this flag is off in the weak and
  2788.      standard modes, and on in the checks and strict modes.)
  2789.  
  2790. shortcut
  2791.  
  2792.      A shortcut flag. This flag sets other flags, so it has no default
  2793.      value.
  2794.  
  2795. Types
  2796.  
  2797. Abstract Types
  2798.  
  2799. plain: -
  2800. impabstract
  2801.  
  2802. Implicit abstract annotation for type declarations that do not use concrete.
  2803.  
  2804. m: -+++
  2805. mutrep
  2806.  
  2807. Representation of mutable type has sharing semantics.
  2808.  
  2809. Access (Section 3.1)
  2810.  
  2811. plain: +
  2812. accessmodule
  2813.  
  2814. An abstract type defined in M.h (or specified in M.lcl) is accessible in
  2815. M.c.
  2816.  
  2817. plain: +
  2818. accessfile
  2819.  
  2820. An abstract type named type is accessible in files named type.<extenstion>.
  2821.  
  2822. plain: +
  2823. accessczech
  2824.  
  2825. An abstract type named type may be accessible in a function named type_name.
  2826. (see Section 9.1.1)
  2827.  
  2828. plain: -
  2829. accessslovak
  2830.  
  2831. An abstract type named type may be accessible in a function named typeName.
  2832. (see Section.9.1.2)
  2833.  
  2834. plain: -
  2835. accessczechoslovak
  2836.  
  2837. An abstract type named type may be accessible in a function named type_name
  2838. or typeName. (see Section 9.1.3)
  2839.  
  2840. shortcut
  2841. accessall
  2842.  
  2843. Sets accessmodule, accessfile and accessczech.
  2844.  
  2845. Boolean Types (Section 3.3)
  2846.  
  2847. These flags control the type name used to represent booleans, and whether
  2848. the boolean type is abstract.
  2849.  
  2850. plain: -
  2851. bool
  2852.  
  2853. Boolean type is an abstract type.
  2854.  
  2855. plain: bool
  2856. booltype <name>
  2857.  
  2858. Set name of boolean type to <name>.
  2859.  
  2860. plain: FALSE
  2861. boolfalse <name>
  2862.  
  2863. Set name of boolean false to <name>.
  2864.  
  2865. plain: TRUE
  2866. booltrue <name>
  2867.  
  2868. Set name of boolean true to <name>.
  2869.  
  2870. Predicates
  2871.  
  2872. m: --++
  2873. predboolptr
  2874.  
  2875. Type of condition test is a pointer.
  2876.  
  2877. m: -+++
  2878. predboolint
  2879.  
  2880. Type of condition test is an integral type.
  2881.  
  2882. m: ++++
  2883. predboolothers
  2884.  
  2885. Type of condition test is not a boolean, pointer or integral type.
  2886.  
  2887. shortcut
  2888. predbool
  2889.  
  2890. Sets predboolint, predboolptr and preboolothers.
  2891.  
  2892. plain: +
  2893. predassign
  2894.  
  2895. The condition test is an assignment expression. If an assignment is
  2896. intended, add an extra parentheses nesting (e.g., if ((a = b)) ...).
  2897.  
  2898. Primitive Operations
  2899.  
  2900. m: ---+
  2901. ptrarith
  2902.  
  2903. Arithmetic involving pointer and integer.
  2904.  
  2905. m: ++--
  2906. ptrnegate
  2907.  
  2908. Allow the operand of the ! operator to be a pointer.
  2909.  
  2910. m: ---+
  2911. strictops
  2912.  
  2913. Primitive operation does not type check strictly.
  2914.  
  2915. m: ---+
  2916. sizeoftype
  2917.  
  2918. Operand of sizeof operator is a type. (Safer to use expression, int *x =
  2919. sizeof (*x); instead of sizeof (int).)
  2920.  
  2921. Format Codes
  2922.  
  2923. plain: +
  2924. formatcode
  2925.  
  2926. Invalid format code in format string for printflike or scanflike function.
  2927.  
  2928. plain: +
  2929. formattype
  2930.  
  2931. Type-mismatch in parameter corresponding to format code in a printflike or
  2932. scanflike function.
  2933.  
  2934. Main
  2935.  
  2936. plain: +
  2937. maintype
  2938.  
  2939. Type of main does not match expected type (function returning an int, taking
  2940. no parameters or two parameters of type int and char **.)
  2941.  
  2942. Comparisons
  2943.  
  2944. m: -+++
  2945. boolcompare
  2946.  
  2947. Comparison between boolean values. This is dangerous since there may be
  2948. multiple TRUE values if any non-zero value is interpreted at TRUE.
  2949.  
  2950. m: -+++
  2951. realcompare
  2952.  
  2953. Comparison involving float or double values. This is dangerous since it may
  2954. produce unexpected results because floating point representations are
  2955. inexact.
  2956.  
  2957. m: -+++
  2958. ptrcompare
  2959.  
  2960. Comparison between pointer and number.
  2961.  
  2962. Type Equivalence
  2963.  
  2964. m: +---
  2965. voidabstract
  2966.  
  2967. Allow void * to match pointers to abstract types. (Casting a pointer to an
  2968. abstract type to a pointer to void is okay if +voidabstract is set.)
  2969.  
  2970. m: +---
  2971. forwarddecl
  2972.  
  2973. Forward declarations of pointers to abstract representation match abstract
  2974. type.
  2975.  
  2976. m: +---
  2977. charindex
  2978.  
  2979. Allow char to index arrays.
  2980.  
  2981. m: ----
  2982. enumindex
  2983.  
  2984. Allow members of enum type to index arrays.
  2985.  
  2986. m: +---
  2987. boolint
  2988.  
  2989. Make bool and int types equivalent. (No type errors are reported when a
  2990. boolean is used where an integral type is expected and vice versa.)
  2991.  
  2992. m: +---
  2993. charint
  2994.  
  2995. Make char and int types equivalent.
  2996.  
  2997. m: ++--
  2998. enumint
  2999.  
  3000. Make enum and int types equivalent.
  3001.  
  3002. m: ----
  3003. ignorequals
  3004.  
  3005. Ignore type qualifiers (long, short, unsigned).
  3006.  
  3007. m: ++--
  3008. relaxquals
  3009.  
  3010. Report qualifier mismatches only if dangerous (information may be lost since
  3011. a larger type is assigned to (or passed as) a smaller one or a comparison
  3012. uses signed and unsigned values.)
  3013.  
  3014. m: ----
  3015. ignoresigns
  3016.  
  3017. Ignore signs in type comparisons (unsigned matches signed).
  3018.  
  3019. m: ++++
  3020. numliteral
  3021.  
  3022. Integer literals can be used as floats.
  3023.  
  3024. m: ++++
  3025. zeroptr
  3026.  
  3027. Literal 0 may be used as a pointer.
  3028.  
  3029. m: ----
  3030. relaxtypes
  3031.  
  3032. Allow all numeric types to match.
  3033.  
  3034. Function Interfaces
  3035.  
  3036. Modification (Section 4.1)
  3037.  
  3038. m: ++++
  3039. modifies
  3040.  
  3041. Undocumented modification of caller-visible state. Without +moduncon,
  3042. modification errors are only reported in the definitions of functions
  3043. declared with a modifies clause (or specified).
  3044.  
  3045. m: --++
  3046. mustmod
  3047.  
  3048. Documented modification is not detected. An object listed in the modifies
  3049. clause for a function, is not modified by the implementation.
  3050.  
  3051. shortcut
  3052. moduncon
  3053.  
  3054. Report modification errors in functions declared without a modifies
  3055. clause.(Sets modnomods, modglobsnomods and modstrictglobsnomods.)
  3056.  
  3057. m: ---+
  3058. modnomods
  3059.  
  3060. Report modification errors (not involving global variables) in functions
  3061. declared without a modifies clause.
  3062.  
  3063. m: ---+
  3064. modunconnomods
  3065.  
  3066. An unconstrained function is called in a function body where modifications
  3067. are checked. Since the unconstrained function may modify anything, there may
  3068. be undetected modifications in the checked function.
  3069.  
  3070. m: ---+
  3071. modinternalstrict
  3072.  
  3073. A function that modifies internalState is called from a function that does
  3074. not list internalState in its modifies clause.
  3075.  
  3076. Global Variables (Section 4.2)
  3077.  
  3078. Errors involving the use and modification of global and file static
  3079. variables are reported depending on flag settings, annotations where the
  3080. global variable is declared, and whether or not the function where the
  3081. global is used was declared with a globals clause.
  3082.  
  3083. m: ++++
  3084. globs
  3085.  
  3086. Undocumented use of a checked global variable in a function with a globals
  3087. list.
  3088.  
  3089. m: ++++
  3090. globuse
  3091.  
  3092. A global listed in the globals list is not used in the implementation.
  3093.  
  3094. m: ---+
  3095. globnoglobs
  3096.  
  3097. Use of a checked global in a function with no globals list.
  3098.  
  3099. m: ---+
  3100. internalglobs
  3101.  
  3102. Undocumented use of internal state (should have globals internalState).
  3103.  
  3104. m: ---+
  3105. internalglobsnoglobs
  3106.  
  3107. Use of internal state in function with no globals list.
  3108.  
  3109. m: -+++
  3110. globstate
  3111.  
  3112. A function returns with global in inconsistent state (null or undefined)
  3113.  
  3114. m: --++
  3115. allglobs
  3116.  
  3117. Report use and modification errors for globals not annotated with unchecked.
  3118.  
  3119. m: ++++
  3120. checkstrictglobs
  3121.  
  3122. Report use and modification errors for checkedstrict globals.
  3123.  
  3124. Modification of Global Variables
  3125.  
  3126. m: -+++
  3127. modglobs
  3128.  
  3129. Undocumented modification of a checked global variable.
  3130.  
  3131. m: ---+
  3132. modglobsunchecked
  3133.  
  3134. Undocumented modification of an unchecked global variable.
  3135.  
  3136. m: ---+
  3137. modglobsnomods
  3138.  
  3139. Undocumented modification of a checked global variable in a function with no
  3140. modifies clause.
  3141.  
  3142. m: ---+
  3143. modstrictglobsnomods
  3144.  
  3145. Undocumented modification of a checkedstrict global variable in a function
  3146. declared with no modifies clause.
  3147.  
  3148. Globals Lists and Modifies Clauses
  3149.  
  3150. m: ---+
  3151. warnmissingglobs
  3152.  
  3153. Global variable used in modifies clause is not listed in globals list. (The
  3154. global is added to the globals list.)
  3155.  
  3156. m: ---+
  3157. warnmissingglobsnoglobs
  3158.  
  3159. Global variable used in modifies clause of a function with no globals list.
  3160.  
  3161. m: --++
  3162. globsimpmodsnothing
  3163.  
  3164. A function declared with a globals list but no modifies clause is assumed to
  3165. modify nothing.
  3166.  
  3167. m: ----
  3168. modsimpnoglobs
  3169.  
  3170. A function declared with a modifies clause but no globals list is assumed to
  3171. use no globals.
  3172.  
  3173. Implicit Checking Qualifiers
  3174.  
  3175. m: ----
  3176. impcheckedglobs
  3177.  
  3178. Implicit checked qualifier on global variables with no checking annotation.
  3179.  
  3180. m: ----
  3181. impcheckedstatics
  3182.  
  3183. Implicit checked qualifier file static scope variables with no checking
  3184. annotation.
  3185.  
  3186. m: ----
  3187. impcheckmodglobs
  3188.  
  3189. Implicit checkmod qualifier on global variables with no checking annotation.
  3190.  
  3191. m: ----
  3192. impcheckmodstatics
  3193.  
  3194. Implicit checkmod qualifier file static scope variables with no checking
  3195. annotation.
  3196.  
  3197. m: ---+
  3198. impcheckedstrictglobs
  3199.  
  3200. Implicit checked qualifier on global variables with no checking annotation.
  3201.  
  3202. m: ---+
  3203. impcheckedstrictstatics
  3204.  
  3205. Implicit checked qualifier file static scope variables with no checking
  3206. annotation.
  3207.  
  3208. m: --++
  3209. impcheckmodinternals
  3210.  
  3211. Implicit checkmod qualifier on function scope static variables with no
  3212. checking annotation.
  3213.  
  3214. m: -+++
  3215. impglobsweak
  3216.  
  3217. Global Aliasing
  3218.  
  3219. shortcut
  3220. globalias
  3221.  
  3222. Function returns with global aliasing external state (sets
  3223. checkstrictglobalias, checkedglobalias, checkmodglobalias and
  3224. uncheckedglobalias).
  3225.  
  3226. m: -+++
  3227. checkstrictglobalias
  3228.  
  3229. Function returns with a checkstrict global aliasing external state.
  3230.  
  3231. m: -+++
  3232. checkedglobalias
  3233.  
  3234. Function returns with a checked global aliasing external state.
  3235.  
  3236. m: -+++
  3237. checkmodglobalias
  3238.  
  3239. Function returns with a checkmod global aliasing external state.
  3240.  
  3241. m: --++
  3242. uncheckedglobalias
  3243.  
  3244. Function returns with an unchecked global aliasing external state.
  3245.  
  3246. Declaration Consistency (Section 4.3)
  3247.  
  3248. m: -+++
  3249. incondefs
  3250.  
  3251. Identifier redeclared or redefined with inconsistent type.
  3252.  
  3253. m: -+++
  3254. incondefslib
  3255.  
  3256. Identifier defined in a library is redefined with inconsistent type
  3257.  
  3258. m: ----
  3259. overload
  3260.  
  3261. Standard library function overloaded.
  3262.  
  3263. m: -+++
  3264. matchfields
  3265.  
  3266. A struct or enum type is redefined with inconsistent fields or members.
  3267.  
  3268. Memory Management
  3269.  
  3270. Reporting of memory management errors is controlled by flags setting
  3271. checking and implicit annotations and code annotations.
  3272.  
  3273. Deallocation Errors (Section 5.2)
  3274.  
  3275. m: -+++
  3276. usereleased
  3277.  
  3278. Storage used after it may have been released.
  3279.  
  3280. m: ---+
  3281. strictusereleased
  3282.  
  3283. An array element used after it may have been released.
  3284.  
  3285. Inconsistent Branches
  3286.  
  3287. m: -+++
  3288. branchstate
  3289.  
  3290. Storage has inconsistent states of alternate paths through a branch (e.g.,
  3291. it is released in the true branch of an if-statement, but there is no else
  3292. branch.)
  3293.  
  3294. m: ---+
  3295. strictbranchstate
  3296.  
  3297. Storage through array fetch has inconsistent states of alternate paths
  3298. through a branch. Since array elements are not checked accurately, this may
  3299. lead to spurious errors.
  3300.  
  3301. m: --++
  3302. deparrays
  3303.  
  3304. Treat array elements as dependent storage. Checking of array elements cannot
  3305. be done accurately by LCLint. If deparrays is not set, array elements are
  3306. assumed to be independent, so code that releases the same element more than
  3307. once will produce no error. If deparrays is set, array elements are assumed
  3308. to be dependent, so code that releases the same element more that once will
  3309. produce an error, but so will code that releases different elements
  3310. correctly will produce a spurious error.
  3311.  
  3312. Memory Leaks
  3313.  
  3314. m: -+++
  3315. mustfree
  3316.  
  3317. Allocated storage was not released before return or scope exit Errors are
  3318. reported for only, fresh or owned storage.
  3319.  
  3320. m: -+++
  3321. compdestroy
  3322.  
  3323. All only references derivable from out only parameter of type void * must be
  3324. released. (This is the type of the parameter to free, but may also be used
  3325. for user-defined deallocation functions.)
  3326.  
  3327. m: ---+
  3328. strictdestroy
  3329.  
  3330. Report complete destruction errors for array elements that may have been
  3331. released. (If strictdestroy is not set, LCLint will assume that if any array
  3332. element was released, the entire array was correctly released.)
  3333.  
  3334. Transfer Errors
  3335.  
  3336. A transfer error is reported when storage is transferred (by an assignment,
  3337. passing a parameter, or returning) in a way that is inconsistent.
  3338.  
  3339. shortcut
  3340. memtrans
  3341.  
  3342. Sets all memory transfer errors flags.
  3343.  
  3344. m: -+++
  3345. onlytrans
  3346.  
  3347. Only storage transferred to non-only reference (memory leak).
  3348.  
  3349. m: -+++
  3350. ownedtrans
  3351.  
  3352. Owned storage transferred to non-owned reference (memory leak).
  3353.  
  3354. m: -+++
  3355. freshtrans
  3356.  
  3357. Newly-allocated storage transferred to non-only reference (memory leak).
  3358.  
  3359. m: -+++
  3360. sharedtrans
  3361.  
  3362. Shared storage transferred to non-shared reference.
  3363.  
  3364. m: -+++
  3365. dependenttrans
  3366.  
  3367. Inconsistent dependent transfer. Dependent storage is transferred to a
  3368. non-dependent reference.
  3369.  
  3370. m: -+++
  3371. temptrans
  3372.  
  3373. Temporary storage (associated with a temp formal parameter) is transferred
  3374. to a non-temporary reference. The storage may be released or new aliases
  3375. created.
  3376.  
  3377. m: -+++
  3378. kepttrans
  3379.  
  3380. Kept storage transferred to non-temporary reference.
  3381.  
  3382. m: -+++
  3383. keeptrans
  3384.  
  3385. Keep storage is transferred in a way that may add a new alias to it, or
  3386. release it.
  3387.  
  3388. m: -+++
  3389. refcounttrans
  3390.  
  3391. Reference counted storage is transferred in an inconsistent way.
  3392.  
  3393. m: -+++
  3394. newreftrans
  3395.  
  3396. A new reference transferred to a reference counted reference (reference
  3397. count is not set correctly).
  3398.  
  3399. m: -+++
  3400. immediatetrans
  3401.  
  3402. An immediate address (result of &) is transferred inconsistently.
  3403.  
  3404. m: -+++
  3405. statictrans
  3406.  
  3407. Static storage is transferred in an inconsistent way.
  3408.  
  3409. m: -+++
  3410. exposetrans
  3411.  
  3412. Inconsistent exposure transfer. Exposed storage is transferred to a
  3413. non-exposed, non-observer reference.
  3414.  
  3415. m: -+++
  3416. observertrans
  3417.  
  3418. Inconsistent observer transfer. Observer storage is transferred to a
  3419. non-observer reference.
  3420.  
  3421. m: -+++
  3422. unqualifiedtrans
  3423.  
  3424. Unqualified storage is transferred in an inconsistent way.
  3425.  
  3426. Derived Storage
  3427.  
  3428. m: -+++
  3429. compmempass
  3430.  
  3431. Storage derivable from a parameter does not match the alias kind expected
  3432. for the formal parameter.
  3433.  
  3434. Stack References
  3435.  
  3436. m: ++++
  3437. stackref
  3438.  
  3439. A stack reference is pointed to by an external reference when the function
  3440. returns. Since the call frame will be destroyed when the function returns
  3441. the return value will point to dead storage. (Section 5.2.6)
  3442.  
  3443. Implicit Memory Annotations (Section 5.3)
  3444.  
  3445. plain: +
  3446. globimponly
  3447.  
  3448. Assume unannotated global storage is only.
  3449.  
  3450. plain: +
  3451. paramimptemp
  3452.  
  3453. Assume unannotated parameter is temp.
  3454.  
  3455. plain: +
  3456. retimponly
  3457.  
  3458. Assume unannotated returned storage is only.
  3459.  
  3460. plain: +
  3461. structimponly
  3462.  
  3463. Assume unannotated structure or union field is only.
  3464.  
  3465. shortcut
  3466. codeimponly
  3467.  
  3468. Sets globimponly, retimponly and structimponly.
  3469.  
  3470. m: -+++
  3471. memimp
  3472.  
  3473. Report memory errors for unqualified storage.
  3474.  
  3475. m: ----
  3476. passunknown
  3477.  
  3478. Passing a value as an unannotated parameter clears its annotation. This will
  3479. prevent many spurious errors from being report for unannotated programs, but
  3480. eliminates the possibility of detecting many errors.
  3481.  
  3482. Sharing
  3483.  
  3484. Aliasing (Section 6)
  3485.  
  3486. m: -+++
  3487. aliasunique
  3488.  
  3489. A actual parameter that is passed as a unique formal parameter is aliased by
  3490. another parameter or global variable.
  3491.  
  3492. m: -+++
  3493. mayaliasunique
  3494.  
  3495. A actual parameter that is passed as a unique formal parameter may be
  3496. aliased by another parameter or global variable.
  3497.  
  3498. m: -+++
  3499. mustnotalias
  3500.  
  3501. An alias has been added to a temp-qualifier parameter or global that is
  3502. visible externally when the function returns.
  3503.  
  3504. m: --++
  3505. retalias
  3506.  
  3507. A function returns an alias to parameter or global.
  3508.  
  3509. Exposure (Section 6.2)
  3510.  
  3511. shortcut
  3512. repexpose
  3513.  
  3514. The internal representation of an abstract type is visible to the caller.
  3515. This means clients may have access to a pointer into the abstract
  3516. representation. (Sets assignexpose, retexpose, and castexpose.)
  3517.  
  3518. m: --++
  3519. assignexpose
  3520.  
  3521. Abstract representation is exposed by an assignment or passed parameter.
  3522.  
  3523. m: --++
  3524. castexpose
  3525.  
  3526. Abstract representation is exposed through a cast.
  3527.  
  3528. m: --++
  3529. retexpose
  3530.  
  3531. Abstract representation is exposed by a return value.
  3532.  
  3533. Observer Modifications
  3534.  
  3535. plain: +
  3536. modobserver
  3537.  
  3538. Possible modification of observer storage.
  3539.  
  3540. m: ---+
  3541. modobserveruncon
  3542.  
  3543. Storage declared with observer may be modified through a call to an
  3544. unconstrained function.
  3545.  
  3546. String Literals (Section 6.2.1)
  3547.  
  3548. m: --++
  3549. readonlytrans
  3550.  
  3551. Report memory transfer errors for initializations to read-only string
  3552. literals
  3553.  
  3554. m: -+++
  3555. readonlystrings
  3556.  
  3557. String literals are read-only (ANSI semantics). An error is reported if a
  3558. string literal may be modified or released.
  3559.  
  3560. Use Before Definition (Section 7.1)
  3561.  
  3562. m: -+++
  3563. usedef
  3564.  
  3565. The value of a location that may not be initialized on some execution path
  3566. is used.
  3567.  
  3568. m: ----
  3569. impouts
  3570.  
  3571. Allow unannotated pointer parameters to functions to be implicit out
  3572. parameters.
  3573.  
  3574. m: -+++
  3575. compdef
  3576.  
  3577. Storage derivable from a parameter, return value or global variable is not
  3578. completely defined.
  3579.  
  3580. m: -+++
  3581. uniondef
  3582.  
  3583. No field of a union is defined. (No error is reported if at least one union
  3584. field is defined.)
  3585.  
  3586. m: -+++
  3587. mustdefine
  3588.  
  3589. Parameter declared with out is not defined before return or scope exit.
  3590.  
  3591. Null Pointers (Section 7.2)
  3592.  
  3593. m: -+++
  3594. null
  3595.  
  3596. A possibly null pointer may be dereferenced, or used somewhere a non-null
  3597. pointer is expected.
  3598.  
  3599. Macros (Section 8)
  3600.  
  3601. These flags control expansion and checking of macro definitions and
  3602. invocations.
  3603.  
  3604. Macro Expansion
  3605.  
  3606. These flags control which macros are checked as functions or constants, and
  3607. which are expanded in the pre-processing phase. Macros preceded by
  3608. /*@notfunction@*/ are never expanded regardless of these flag settings.
  3609. These flags may be used in source-file control comments.
  3610.  
  3611. plain: -
  3612. fcnmacros
  3613.  
  3614. Macros defined with parameter lists are not expanded and are checked as
  3615. functions.
  3616.  
  3617. plain: -
  3618. constmacros
  3619.  
  3620. Macros defined without parameter lists are not expanded and are checked as
  3621. constants.
  3622.  
  3623. shortcut
  3624. allmacros
  3625.  
  3626. Sets allfcnmacros and allconstmacros.
  3627.  
  3628. plain: -
  3629. libmacros
  3630.  
  3631. Macros defining identifiers declared in a loaded library are not expanded
  3632. and are checked according to the library information.
  3633.  
  3634. Macro Definitions
  3635.  
  3636. These flags control what errors are reported in macro definitions.
  3637.  
  3638. m: -+++
  3639. macrostmt
  3640.  
  3641. Macro definition is not syntactically equivalent to function. This means if
  3642. the macro is used as a statement (e.g., if (test) macro();) unexpected
  3643. behavior may result. One fix is to surround the macro body with do { ...; }
  3644. while (FALSE).
  3645.  
  3646. m: -+++
  3647. macroparams
  3648.  
  3649. A macro parameter is not used exactly once in all possible invocations of
  3650. the macro.
  3651.  
  3652. m: -+++
  3653. macroassign
  3654.  
  3655. A macro parameter is used as the left side of an assignment expression.
  3656.  
  3657. m: -+++
  3658. macroparens
  3659.  
  3660. A macro parameter is used without parentheses (in potentially dangerous
  3661. context).
  3662.  
  3663. m: ---+
  3664. macroempty
  3665.  
  3666. Macro definition of a function is empty.
  3667.  
  3668. m: -+++
  3669. macroredef
  3670.  
  3671. Macro is redefined. There is another macro defined with the same name.
  3672.  
  3673. m: -+++
  3674. macrounrecog
  3675.  
  3676. An unrecognized identifier appears in a macro definition. Since the
  3677. identifier may be defined where the macro is used, this could be okay, but
  3678. LCLint will not be able to check the unrecognized identifier appropriately.
  3679.  
  3680. Corresponding Declarations
  3681.  
  3682. m: ++++
  3683. macromatchname
  3684.  
  3685. A iter or constant macro is defined using a different name from the one used
  3686. in the previous syntactic comment.
  3687.  
  3688. shortcut
  3689. macrodecl
  3690.  
  3691. A macro definition has no corresponding declaration. (Sets macrofcndecl and
  3692. macroconstdecl.)
  3693.  
  3694. m: -+++
  3695. macrofcndecl
  3696.  
  3697. Macro definition with parameter list has no corresponding function
  3698. prototype. Without a prototype, the types of the macro result and parameters
  3699. is unknown.
  3700.  
  3701. m: -+++
  3702. macroconstdecl
  3703.  
  3704. A macro definition without parameter list has no corresponding constant
  3705. declaration.
  3706.  
  3707. plain: +
  3708. nextlinemacros
  3709.  
  3710. A constant or iter declaration is not immediately followed by a macro
  3711. definition.
  3712.  
  3713. Side-Effect Free Parameters (Section 8.2.1)
  3714.  
  3715. These flags control error reporting for parameters with inconsistent
  3716. side-effects in invocations of checked function macros and function calls.
  3717.  
  3718. m: -+++
  3719. sefparams
  3720.  
  3721. An actual parameter with side-effects is passed as a formal parameter
  3722. declared with sef.
  3723.  
  3724. m: --++
  3725. sefuncon
  3726.  
  3727. An actual parameter involving a call to an unconstrained function (declared
  3728. without modifies clause) that may modify anything is passed as a sef
  3729. parameter.
  3730.  
  3731. Iterators
  3732.  
  3733. plain: -
  3734. hasyield
  3735.  
  3736. An iterator has been declared with no parameters annotated with yield.
  3737.  
  3738. Naming Conventions
  3739.  
  3740. plain: +
  3741. namechecks
  3742.  
  3743. Turns all name checking on or off without changing other settings.
  3744.  
  3745. Type-Based Naming Conventions (Section 9.1)
  3746.  
  3747. Czech Naming Convention
  3748.  
  3749. shortcut
  3750. czech
  3751.  
  3752. Selects complete Czech naming convention (sets accessczech, czechfcns,
  3753. czechvars, czechconsts, czechmacros, and czechtypes).
  3754.  
  3755. plain: +
  3756. accessczech
  3757.  
  3758. Allow access to abstract types following Czech naming convention. The
  3759. representation of an abstract type named t is accessible in the definition
  3760. of a function or constant named t_name.
  3761.  
  3762. plain: -
  3763. czechfcns
  3764.  
  3765. Function or iterator name is not consistent with Czech naming convention.
  3766.  
  3767. plain: -
  3768. czechvars
  3769.  
  3770. Variable name is not consistent with Czech naming convention.
  3771.  
  3772. plain: -
  3773. czechmacros
  3774.  
  3775. Expanded macro name is not consistent with Czech naming convention.
  3776.  
  3777. plain: -
  3778. czechconsts
  3779.  
  3780. Constant name is not consistent with Czech naming convention.
  3781.  
  3782. plain: -
  3783. czechtypes
  3784.  
  3785. Type name is not consistent with Czech naming convention. Czech type names
  3786. must not use the underscore character.
  3787.  
  3788. Slovak Naming Convention
  3789.  
  3790. shortcut
  3791. slovak
  3792.  
  3793. Selects complete Slovak naming convention (sets accessslovak, slovakfcns,
  3794. slovakvars, slovakconsts, slovakmacros, and slovaktypes).
  3795.  
  3796. plain: -
  3797. accessslovak
  3798.  
  3799. Allow access to abstract types following Slovak naming convention. The
  3800. representation of an abstract type named t is accessible in the definition
  3801. of a function or constant named tName.
  3802.  
  3803. plain: -
  3804. slovakfcns
  3805.  
  3806. Function or iterator name is not consistent with Slovak naming convention.
  3807.  
  3808. plain: -
  3809. slovakmacros
  3810.  
  3811. Expanded macro name is not consistent with Slovak naming convention.
  3812.  
  3813. plain: -
  3814. slovakvars
  3815.  
  3816. Variable name is not consistent with Slovak naming convention.
  3817.  
  3818. plain: -
  3819. slovakconsts
  3820.  
  3821. Constant name is not consistent with Slovak naming convention.
  3822.  
  3823. plain: -
  3824. slovaktypes
  3825.  
  3826. Type name is not consistent with Slovak naming convention. Slovak type names
  3827. may not include uppercase letters.
  3828.  
  3829. Czechoslovak Naming Convention
  3830.  
  3831. shortcut
  3832. czechoslovak
  3833.  
  3834. Selects complete Czechoslovak naming convention (sets accessczechoslovak,
  3835. czechoslovakfcns, czechoslovakvars, czechoslovakconsts, czechoslovakmacros,
  3836. and czechoslovaktypes).
  3837.  
  3838. plain: -
  3839. accessczechoslovak
  3840.  
  3841. Allow access to abstract types by Czechoslovak naming convention. The
  3842. representation of an abstract type named t is accessible in the definition
  3843. of a function or constant named t_name or tName.
  3844.  
  3845. plain: -
  3846. czechoslovakfcns
  3847.  
  3848. Function name is not consistent with Czechoslovak naming convention.
  3849.  
  3850. plain: -
  3851. czechoslovakmacros
  3852.  
  3853. Expanded macro name is not consistent with Czechoslovak naming convention.
  3854.  
  3855. plain: -
  3856. czechoslovakvars
  3857.  
  3858. Variable name is not consistent with Czechoslovak naming convention.
  3859.  
  3860. plain: -
  3861. czechoslovakconsts
  3862.  
  3863. Constant name is not consistent with Czechoslovak naming convention.
  3864.  
  3865. plain: -
  3866. czechoslovaktypes
  3867.  
  3868. Type name is not consistent with Czechoslovak naming convention.
  3869. Czechoslovak type names may not include uppercase letters or the underscore
  3870. character.
  3871.  
  3872. Namespace Prefixes (Section 9.2)
  3873.  
  3874. macrovarprefix <prefix string>
  3875.  
  3876. Set namespace prefix for variables declared in a macro body. (Default is
  3877. m_.)
  3878.  
  3879. plain: +
  3880. macrovarprefixexclude
  3881.  
  3882. A variable declared outside a macro body starts with the macrovarprefix.
  3883.  
  3884. tagprefix <prefix string>
  3885.  
  3886. Set namespace prefix of struct, union or enum tag identifiers.
  3887.  
  3888. plain: -
  3889. tagprefixexclude
  3890.  
  3891. An identifier that is not a tag starts with the tagprefix.
  3892.  
  3893. enumprefix <prefix string>
  3894.  
  3895. Set namespace prefix for enum members.
  3896.  
  3897. plain: -
  3898. enumprefixexclude
  3899.  
  3900. An identifier that is not an enum member starts with the enumprefix.
  3901.  
  3902. filestaticprefix <prefix string>
  3903.  
  3904. Set namespace prefix for file static declarations.
  3905.  
  3906. plain: -
  3907. filestaticprefixexclude
  3908.  
  3909. An identifier that is not file static starts with the filestaticprefix.
  3910.  
  3911. globalprefix <prefix string>
  3912.  
  3913. Set namespace prefix for global variables.
  3914.  
  3915. plain: -
  3916. globalprefixexclude
  3917.  
  3918. An identifier that is not a global variable starts with the globalprefix.
  3919.  
  3920. typeprefix <prefix string>
  3921.  
  3922. Set namespace prefix for user-defined types.
  3923.  
  3924. plain: -
  3925. typeprefixexclude
  3926.  
  3927. An identifier that is not a type name starts with the typeprefix.
  3928.  
  3929. externalprefix <prefix string>
  3930.  
  3931. Set namespace prefix for external identifiers.
  3932.  
  3933. plain: -
  3934. externalprefixexclude
  3935.  
  3936. An identifier that is not external starts with the externalprefix.
  3937.  
  3938. localprefix <prefix string>
  3939.  
  3940. Set namespace prefix for local variables.
  3941.  
  3942. plain: -
  3943. localprefixexclude
  3944.  
  3945. An identifier that is not a local variable starts with the localprefix.
  3946.  
  3947. uncheckedmacroprefix <prefix string>
  3948.  
  3949. Set namespace prefix for unchecked macros.
  3950.  
  3951. plain: -
  3952. uncheckedmacroprefixexclude
  3953.  
  3954. An identifier that is not the name of an unchecked macro starts with the
  3955. uncheckedmacroprefix.
  3956.  
  3957. constprefix <prefix string>
  3958.  
  3959. Set namespace prefix for constants.
  3960.  
  3961. plain: -
  3962. constprefixexclude
  3963.  
  3964. An identifier that is not a constant starts with the constantprefix.
  3965.  
  3966. iterprefix <prefix string>
  3967.  
  3968. Set namespace prefix for iterators.
  3969.  
  3970. plain: -
  3971. iterprefixexclude
  3972.  
  3973. An identifier that is not a iter starts with the iterprefix.
  3974.  
  3975. Naming Restrictions (Section 9.3)
  3976.  
  3977. m: -+++
  3978. shadow
  3979.  
  3980. Declaration reuses name visible in outer scope.
  3981.  
  3982. Reserved Names
  3983.  
  3984. m: --++
  3985. ansireserved
  3986.  
  3987. External name conflicts with name reserved for the compiler or standard
  3988. library.
  3989.  
  3990. m: ---+
  3991. ansireservedinternal
  3992.  
  3993. Internal name conflicts with name reserved for the compiler or standard
  3994. library.
  3995.  
  3996. Distinct External Names
  3997.  
  3998. plain: -
  3999. distinctexternalnames
  4000.  
  4001. An external name is not distinguishable from another external name using
  4002. externalnamelen significant characters.
  4003.  
  4004. externalnamelen<number>
  4005.  
  4006. Sets the number of significant characters in an external name (ANSI default
  4007. minimum is 6). Sets +distinctexternalnames.
  4008.  
  4009. plain: -
  4010. externalnamecaseinsensitive
  4011.  
  4012. Make alphabetic case insignificant in external names. According to ANSI
  4013. standard, case need not be significant in an external name. If
  4014. +distinctexternalnames is not set, sets +distinctexternalnames with
  4015. unlimited external name length.
  4016.  
  4017. Distinct Internal Names
  4018.  
  4019. m: ----
  4020. distinctinternalnames
  4021.  
  4022. An internal name is not distinguishable from another internal name using
  4023. internalnamelen significant characters. (Also effected by
  4024. internalnamecaseinsensitive and internalnamelookalike.)
  4025.  
  4026. internalnamelen <number>
  4027.  
  4028. Set the number of significant characters in an internal name. Sets
  4029. +distinctinternalnames.
  4030.  
  4031. plain: -
  4032. internalnamecaseinsensitive
  4033.  
  4034. Set whether case is significant an internal names
  4035. (-internalnamecaseinsensitive means case is significant). If
  4036. +distinctinternalnames is not set, sets +distinctinternalnames with
  4037. unlimited internal name length.
  4038.  
  4039. plain: -
  4040. internalnamelookalike
  4041.  
  4042. Set whether similar looking characters (e.g., "1" and "l") match in internal
  4043. names.
  4044.  
  4045. Other Checks
  4046.  
  4047. Undefined Evaluation Order (Section 10.1)
  4048.  
  4049. m: -+++
  4050. evalorder
  4051.  
  4052. Behavior of an expression is undefined because sub-expressions contain
  4053. interfering side effects that may be evaluated in any order.
  4054.  
  4055. m: ---+
  4056. evalorderuncon
  4057.  
  4058. An expression may be undefined because a sub-expression contains a call to
  4059. an unconstrained function (no modifies clause) that may modify something
  4060. that may be modified or used by another sub-expression.
  4061.  
  4062. Problematic Control Structures (Section 10.2)
  4063.  
  4064. m: -+++
  4065. infloops
  4066.  
  4067. Likely infinite loop is detected (Section 10.2.1).
  4068.  
  4069. m: --++
  4070. infloopsuncon
  4071.  
  4072. Likely infinite loop is detected. Loop test or body calls an unconstrained
  4073. function, that may produce an undetected modification.
  4074.  
  4075. m: ---+
  4076. elseifcomplete
  4077.  
  4078. There is no final else following an else if construct (Section 10.2.5).
  4079.  
  4080. m: -+++
  4081. casebreak
  4082.  
  4083. These is a non-empty case in a switch not followed by a break (Section
  4084. 10.2.2).
  4085.  
  4086. m: -+++
  4087. misscase
  4088.  
  4089. A switch on an enum type is missing a case for a member of the enumerator.
  4090.  
  4091. m: ----
  4092. loopexec
  4093.  
  4094. Assume all loops execute at least once. This effects use-before-definition
  4095. and memory checking. It should probably not be used globally, but may be
  4096. used surrounding a particular loop that is known to always execute to
  4097. prevent spurious messages.
  4098.  
  4099. Deep Break (Section 10.2.3)
  4100.  
  4101. shortcut
  4102. deepbreak
  4103.  
  4104. Report errors for break statements inside a nested while, for or switch.
  4105. (Sets all nested break and continue flags.)
  4106.  
  4107. m: --++
  4108. looploopbreak
  4109.  
  4110. There is a break inside a while, for or iterator loop that is inside a
  4111. while, for or iterator loop. Mark with /*@innerbreak@*/ to suppress the
  4112. message.
  4113.  
  4114. m: --++
  4115. switchloopbreak
  4116.  
  4117. There is a break inside a while, for or iterator loop that is inside a
  4118. switch statement. Mark with /*@loopbreak@*/.
  4119.  
  4120. m: ---+
  4121. loopswitchbreak
  4122.  
  4123. There is a break inside a switch statement that is inside a while, for or
  4124. iterator loop. Mark with /*@switchbreak@*/.
  4125.  
  4126. m: ---+
  4127. switchswitchbreak
  4128.  
  4129. There is a break inside a switch statement that is inside another switch
  4130. statement. Mark with /*@innerbreak@*/.
  4131.  
  4132. m: ---+
  4133. looploopcontinue
  4134.  
  4135. There is a continue inside a while, for or iterator loop that is inside a
  4136. while, for or iterator loop. Mark with /*@innercontinue@*/.
  4137.  
  4138. Loop and if Bodies (Section 10.2.4)
  4139.  
  4140. shortcut
  4141. allempty
  4142.  
  4143. An if, while or for statement has no body (sets ifempty, whileempty and
  4144. forempty.)
  4145.  
  4146. shortcut
  4147. allblock
  4148.  
  4149. The body of an if, while or for statement is not a block (sets ifblock,
  4150. whileblock and forblock.)
  4151.  
  4152. m: --++
  4153. whileempty
  4154.  
  4155. A while statement has no body.
  4156.  
  4157. m: ---+
  4158. whileblock
  4159.  
  4160. The body of a while statement is not a block
  4161.  
  4162. m: ---+
  4163. forempty
  4164.  
  4165. A for statement has no body.
  4166.  
  4167. m: ---+
  4168. forblock
  4169.  
  4170. The body of a for statement is not a block.
  4171.  
  4172. m: ++++
  4173. ifempty
  4174.  
  4175. An if statement has no body.
  4176.  
  4177. m: ---+
  4178. ifblock
  4179.  
  4180. The body of an if statement is not a block.
  4181.  
  4182. Suspicious Statements (Section 10.3)
  4183.  
  4184. m: -+++
  4185. unreachable
  4186.  
  4187. Code is not reached on any possible execution.
  4188.  
  4189. m: -+++
  4190. noeffect
  4191.  
  4192. Statement has no effect.
  4193.  
  4194. m: ---+
  4195. noeffectuncon
  4196.  
  4197. Statement involving call to unconstrained function may have no effect.
  4198.  
  4199. m: -+++
  4200. noret
  4201.  
  4202. There is a path with no return in a function declared to return a non-void
  4203. value.
  4204.  
  4205. Ignored Return Values (Section 10.3.2)
  4206.  
  4207. These flags control when errors are reported for function calls that do not
  4208. use the return value. Casting the function call to void or declaring the
  4209. called function to return /*@alt void@*/.
  4210.  
  4211. m: -+++
  4212. retvalbool
  4213.  
  4214. Return value of type bool ignored.
  4215.  
  4216. m: -+++
  4217. retvalint
  4218.  
  4219. Return value of type int ignored.
  4220.  
  4221. m: ++++
  4222. retvalother
  4223.  
  4224. Return value of type other than bool or int ignored.
  4225.  
  4226. shortcut
  4227. retval
  4228.  
  4229. Return value ignored (Sets retvalbool, retvalint, retvalother.)
  4230.  
  4231. Unused Declarations (Section 10.4)
  4232.  
  4233. These flags control when errors are reported for declarations that are never
  4234. used. The unused annotation can be used to prevent unused errors from being
  4235. report for a particular declaration.
  4236.  
  4237. m: ---+
  4238. topuse
  4239.  
  4240. A external declaration is not used in any file.
  4241.  
  4242. m: -+++
  4243. constuse
  4244.  
  4245. Constant never used.
  4246.  
  4247. m: -+++
  4248. enummemuse
  4249.  
  4250. Member of enumerator never used.
  4251.  
  4252. m: ++++
  4253. varuse
  4254.  
  4255. Variable never used.
  4256.  
  4257. m: -+++
  4258. paramuse
  4259.  
  4260. Function parameter never used.
  4261.  
  4262. m: ++++
  4263. fcnuse
  4264.  
  4265. Function is never used.
  4266.  
  4267. m: ++++
  4268. typeuse
  4269.  
  4270. Defined type never used.
  4271.  
  4272. m: -+++
  4273. fielduse
  4274.  
  4275. Field of structure or union type is never used.
  4276.  
  4277. m: ---+
  4278. unusedspecial
  4279.  
  4280. Declaration in a special file (corresponding to .l or .y file) is unused.
  4281.  
  4282. Complete Programs (Section 10.5)
  4283.  
  4284. m: --++
  4285. declundef
  4286.  
  4287. Function, variable, iterator or constant declared but never defined.
  4288.  
  4289. shortcut
  4290. partial
  4291.  
  4292. Check as partial system (sets -declundef, -exportlocal and prevents checking
  4293. of macros in headers without corresponding .c files.)
  4294.  
  4295. Exports
  4296.  
  4297. m: ---+
  4298. exportlocal
  4299.  
  4300. A declaration is exported but not used outside this module. (Declaration can
  4301. use the static qualifier.)
  4302.  
  4303. m: --++
  4304. exportheader
  4305.  
  4306. A declaration (other than a variable) is exported but does not appear in a
  4307. header file.
  4308.  
  4309. m: --++
  4310. exportheadervar
  4311.  
  4312. A variable declaration is exported but does not appear in a header file.
  4313.  
  4314. Unrecognized Identifiers
  4315.  
  4316. plain: +
  4317. unrecog
  4318.  
  4319. An unrecognized identifier is used.
  4320.  
  4321. plain: +
  4322. systemunrecog
  4323.  
  4324. Report unrecognized identifiers that start with the system prefix, __ (two
  4325. underscores).
  4326.  
  4327. plain: -
  4328. repeatunrecog
  4329.  
  4330. Report multiple messages for unrecognized identifiers. If repeatunrecog is
  4331. not set, an error is reported only the first time a particular unrecognized
  4332. identifier appears in the file.
  4333.  
  4334. Multiple Definition and Declarations
  4335.  
  4336. plain: +
  4337. redef
  4338.  
  4339. A function or variable is defined more than once.
  4340.  
  4341. m: --++
  4342. redecl
  4343.  
  4344. An identifier is declared more than once.
  4345.  
  4346. ANSI C Conformance
  4347.  
  4348. m: --++
  4349. noparams
  4350.  
  4351. A function is declared without a parameter list prototype.
  4352.  
  4353. m: ---+
  4354. oldstyle
  4355.  
  4356. Function definition is in old style syntax. Standard prototype syntax is
  4357. preferred.
  4358.  
  4359. m: -+++
  4360. exitarg
  4361.  
  4362. Argument to exit has implementation defined behavior. The only valid
  4363. arguments to exit are EXIT_SUCCESS, EXIT_FAILURE and 0. An error is reported
  4364. if LCLint can detect statically that the argument to exit is not one of
  4365. these.
  4366.  
  4367. Limits (Section 10.6)
  4368.  
  4369. shortcut
  4370. ansilimits
  4371.  
  4372. Check for violations of standard limits (Sets controlnestdepth,
  4373. stringliterallen, includenest, numstructfields, and numenummembers).
  4374.  
  4375. m: ---+
  4376.  
  4377. controlnestdepth <number>
  4378.  
  4379. Set maximum nesting depth of compound statements, iteration control
  4380. structures, and selection control structures (ANSI minimum is 15).
  4381.  
  4382. m: ---+
  4383. stringliterallen <number>
  4384.  
  4385. Set maximum length of string literals (ANSI minimum is 509).
  4386.  
  4387. m: ---+
  4388. numstructfields <number>
  4389.  
  4390. Set maximum number of fields in a struct or union (ANSI minimum is 127).
  4391.  
  4392. m: ---+
  4393. numenummembers <number>
  4394.  
  4395. Set maximum number of members of an enum type (ANSI minimum is 127).
  4396.  
  4397. m: --++
  4398. includenest <number>
  4399.  
  4400. Set maximum number of nested #include files (ANSI minimum is 8).
  4401.  
  4402. Header Inclusion (Apppendix F)
  4403.  
  4404. singleinclude
  4405.  
  4406. Optimize header inclusion to only include each header file once.
  4407.  
  4408. neverinclude
  4409.  
  4410. Use library information instead of including header files.
  4411.  
  4412. skipsysheaders
  4413.  
  4414. Do not include header files in system directories (as set by -systemdirs)
  4415.  
  4416. m: ---+
  4417. systemdirerrors
  4418.  
  4419. Report errors in files in system directories (set by -systemdirs).
  4420.  
  4421. Comments
  4422.  
  4423. These flags control how syntactic comments are interpreted (see Apppendix
  4424. E).
  4425.  
  4426. commentchar <char>
  4427.  
  4428. Set the marker character for syntactic comments. Comments beginning with
  4429. /*<char> are interpreted by LCLint. Default: @
  4430.  
  4431. plain: -
  4432. noaccess
  4433.  
  4434. Ignore access comments.
  4435.  
  4436. plain: -
  4437. nocomments
  4438.  
  4439. Ignore all stylized comments.
  4440.  
  4441. plain: +
  4442. supcounts
  4443.  
  4444. Actual number of errors does not match number in /*@i<n>@*/
  4445.  
  4446. plain: +
  4447. lintcomments
  4448.  
  4449. Interpret traditional lint comments (/*FALLTHROUGH*/, /*NOTREACHED*/,
  4450. /*PRINTLIKE*/).
  4451.  
  4452. m: -+++
  4453. warnlintcomments
  4454.  
  4455. Print a warning and suggest an alternative when a traditional lint comment
  4456. is used.
  4457.  
  4458. General Checks
  4459.  
  4460. These flags should probably not be set globally since the turn off general
  4461. checks that should always be done. They may be used locally to suppress
  4462. spurious errors.
  4463.  
  4464. plain: +
  4465. abstract
  4466.  
  4467. A data abstraction barrier is violated.
  4468.  
  4469. plain: +
  4470. control
  4471.  
  4472. A control flow error is detected.
  4473.  
  4474. plain: +
  4475. syntax
  4476.  
  4477. Parse error.
  4478.  
  4479. plain: -
  4480. trytorecover
  4481.  
  4482. Try to recover from a parse error. If trytorecover is not set, LCLint will
  4483. abort checking after a parse error is detected. If it is set, LCLint will
  4484. attempt to recover, but LCLint does performs only minimal error recovery.
  4485.  
  4486. plain: +
  4487. type
  4488.  
  4489. Type mismatch.
  4490.  
  4491. Flag Name Abbreviations
  4492.  
  4493. Within a flag name, abbreviations may be used. Table 2 shows the flag name
  4494. abbreviations. The expanded and short forms are interchangeable in flag
  4495. names.
  4496.  
  4497. For example, globsimpmodsnothing and globalsimpliesmodifiesnothing denote
  4498. the same flag. Abbreviations in flag names allow pronounceable, descriptive
  4499. names to be used without making flag names excessively long (although one
  4500. must admit even globsimpmodsnothing is a bit of a mouthful.)
  4501.  
  4502.  Expanded Form          Short Form
  4503.  constant               const
  4504.  declaration            decl
  4505.  function               fcn
  4506.  global                 glob
  4507.  implicit, implied      imp
  4508.  iterator               iter
  4509.  length                 len
  4510.  modifies               mods
  4511.  modify                 mod
  4512.  memory                 mem
  4513.  parameter              param
  4514.  pointer                ptr
  4515.  return                 ret
  4516.  variable               var
  4517.  unconstrained, unconst uncon
  4518.  
  4519. Table 2. Flag name abbreviations.
  4520.  
  4521. Appendix D Annotations
  4522.  
  4523. The grammar below is the C syntax from [K&R,A13] modified to show the syntax
  4524. of syntactic comments. Only productions effected by LCLint annotations are
  4525. shown. In the annotations, the @ represents the comment marker char, set by
  4526. -commentchar (default is @).
  4527.  
  4528. Functions
  4529.  
  4530. direct-declarator:
  4531.  
  4532.           direct-declarator (parameter-type-list_opt) globals_opt modifies_opt
  4533.         | direct-declarator (identifier-list_opt) globals_opt modifies_opt
  4534.  
  4535. globals: (Section 4.2)
  4536.  
  4537.           /*@globals globitem,+ ;_opt @*/
  4538.         | /*@globals declaration-list_opt ;_opt @*/
  4539.  
  4540. globitem:
  4541.  
  4542.            globannot* identifier
  4543.         |  internalState
  4544.         |  systemState
  4545.  
  4546. globannot: undef | killed
  4547.  
  4548. modifies: (Section 4.1)
  4549.  
  4550.            /*@modifies moditem,+;_opt @*/
  4551.          | /*@modifies nothing ;_opt @*/
  4552.          | /*@*/  (Abbreviation for no globals and modifies nothing.)
  4553.  
  4554. moditem:
  4555.  
  4556.            expression
  4557.          | internalState
  4558.          | systemState
  4559.  
  4560. Iterators (Section 8.4)
  4561.  
  4562. The globals and modifies clauses for an iterator are the same as those for a
  4563. function, except they are not enclosed by a comment, since the iterator is
  4564. already a comment.
  4565.  
  4566. direct-declarator:
  4567.  
  4568.        /*@iter identifier (parameter-type-list_opt) globals_opt
  4569. modifies_opt @*/
  4570.  
  4571. Constants (Section 8.1)
  4572.  
  4573. external-declaration:
  4574.  
  4575.        /*@constant declaration ;_opt @*/
  4576.  
  4577. Alternate Types (Section 8.2.2)
  4578.  
  4579. Alternate types may be used in the type specification of parameters and
  4580. return values.
  4581.  
  4582. extended-type:
  4583.  
  4584.         type-specifier alt-type_opt
  4585.  
  4586. alt-type:
  4587.  
  4588.         /*@alt basic-type,+ @*/
  4589.  
  4590. Declarator Annotations
  4591.  
  4592. General annotations appear after storage-class-specifiers and before
  4593. type-specifiers. Multiple annotations may be used in any order. Here,
  4594. annotations are without the surrounding comment. In a declaration, the
  4595. annotation would be surrounded by /*@ and @*/. In a globals or modifies
  4596. clause or iterator or constant declaration, no surrounding comment would be
  4597. used since they are within a comment.
  4598.  
  4599. Type Definitions (Section 3)
  4600.  
  4601. A type definition may use any either abstract or concrete, either mutable or
  4602. immutable, and refcounted. Only a pointer to a struct may be declared with
  4603. refcounted. Mutability annotations may not be used with concrete types since
  4604. concrete types inherit their mutability from the actual type.
  4605.  
  4606. abstract
  4607.  
  4608. Type is abstract (representation is hidden from clients).
  4609.  
  4610. concrete
  4611.  
  4612. Type is concrete (representation is visible to clients).
  4613.  
  4614. immutable
  4615.  
  4616. Instances of the type cannot change value. (Section 3.2)
  4617.  
  4618. mutable
  4619.  
  4620. Instances of the type can change value. (Section 3.2)
  4621.  
  4622. refcounted
  4623.  
  4624. Reference counted type. (Section 5.4)
  4625.  
  4626. Global Variables (Section 4.2.1)
  4627.  
  4628. One check annotation may be used on a global or file-static variable
  4629. declaration.
  4630.  
  4631. unchecked
  4632.  
  4633. Weakest checking for global use.
  4634.  
  4635. checkmod
  4636.  
  4637. Check modification by not use of global.
  4638.  
  4639. checked
  4640.  
  4641. Check use and modification of global.
  4642.  
  4643. checkedstrict
  4644.  
  4645. Check use of global, even in functions with no global list.
  4646.  
  4647. Memory Management (Section 5)
  4648.  
  4649. dependent
  4650.  
  4651. A reference to externally-owned storage. (Section 5.2.2)
  4652.  
  4653. keep
  4654.  
  4655. A parameter that is kept by the called function. The caller may use the
  4656. storage after the call, but the called function is responsible for making
  4657. sure it is deallocated. (Section 5.2.4)
  4658.  
  4659. killref
  4660.  
  4661. A refcounted parameter. This reference is killed by the call. (Section 5.4)
  4662.  
  4663. only
  4664.  
  4665. A unshared reference. Associated memory must be released before reference is
  4666. lost. (Section 5.2)
  4667.  
  4668. owned
  4669.  
  4670. Storage may be shared by dependent references, but associated memory must be
  4671. released before this reference is lost. (Section 5.2.2)
  4672.  
  4673. shared
  4674.  
  4675. Shared reference that is never deallocated. (Section 5.2.5)
  4676.  
  4677. temp
  4678.  
  4679. A temporary parameter. May not be released, and new aliases to it may not be
  4680. created. (Section 5.2.2)
  4681.  
  4682. Aliasing (Section 6)
  4683.  
  4684. Both alias annotations may be used on a parameter declaration.
  4685.  
  4686. unique
  4687.  
  4688. Parameter that may not be aliased by any other reference visible to the
  4689. function. (Section 6.1.1)
  4690.  
  4691. returned
  4692.  
  4693. Parameter that may be aliased by the return value. (Section 6.1.2)
  4694.  
  4695. Exposure (Section 6.2)
  4696.  
  4697. observer
  4698.  
  4699. Reference that cannot be modified. (Section 6.2.1)
  4700.  
  4701. exposed
  4702.  
  4703. Exposed reference to storage in another object. (Section 6.2.1)
  4704.  
  4705. Definition State (Section 7.1)
  4706.  
  4707. out
  4708.  
  4709. Storage reachable from reference need not be defined.
  4710.  
  4711. in
  4712.  
  4713. All storage reachable from reference must be defined.
  4714.  
  4715. partial
  4716.  
  4717. Partially defined. A structure may have undefined fields. No errors reported
  4718. when fields are used.
  4719.  
  4720. reldef
  4721.  
  4722. Relax definition checking. No errors when reference is not defined, or when
  4723. it is used.
  4724.  
  4725. Global State (Section 7.1.4)
  4726.  
  4727. These annotations may only be used in globals lists. Both annotations may be
  4728. used for the same variable, to mean the variable is undefined before and
  4729. after the call.
  4730.  
  4731. undef
  4732.  
  4733. Variable is undefined before the call.
  4734.  
  4735. killed
  4736.  
  4737. Variable is undefined after the call.
  4738.  
  4739. Null State (Section 7.2)
  4740.  
  4741. null
  4742.  
  4743. Possibly null pointer.
  4744.  
  4745. notnull
  4746.  
  4747. Non-null pointer.
  4748.  
  4749. relnull
  4750.  
  4751. Relax null checking. No errors when NULL is assigned to it, or when it is
  4752. used as a non-null pointer.
  4753.  
  4754. Null Predicates (Section 7.2.1)
  4755.  
  4756. A null predicate annotation may be used of the return value of a function
  4757. returning a boolean type, taking a possibly-null pointer for its first
  4758. argument.
  4759.  
  4760. truenull
  4761.  
  4762. If result is TRUE, first parameter is NULL.
  4763.  
  4764. falsenull
  4765.  
  4766. If result is TRUE, first parameter is not NULL.
  4767.  
  4768. Execution (Section 7.3)
  4769.  
  4770. The exits, mayexit and neverexits annotations may be used on any function.
  4771. The trueexit and falseexit annotations may only be used on functions whose
  4772. first argument is a boolean.
  4773.  
  4774. exits
  4775.  
  4776. Function never returns.
  4777.  
  4778. mayexit
  4779.  
  4780. Function may or may not return.
  4781.  
  4782. trueexit
  4783.  
  4784. Function does not return if first parameter is TRUE.
  4785.  
  4786. falseexit
  4787.  
  4788. Function does not return if first parameter if FALSE.
  4789.  
  4790. neverexit
  4791.  
  4792. Function always returns.
  4793.  
  4794. Side-Effects (Section 8.2.1)
  4795.  
  4796. sef
  4797.  
  4798. Corresponding actual parameter has no side effects.
  4799.  
  4800. Declaration
  4801.  
  4802. These annotations can be used on a declaration to control unused or
  4803. undefined error reporting.
  4804.  
  4805. unused
  4806.  
  4807. Identifier need not be used (no unused errors reported.) (Section 10.4)
  4808.  
  4809. external
  4810.  
  4811. Identifier is defined externally (no undefined error reported.) (Section
  4812. 10.5)
  4813.  
  4814. Case
  4815.  
  4816. fallthrough
  4817.  
  4818. Fall-through case. No message is reported if the previous case may
  4819. fall-through into the one immediately after the fallthrough.
  4820.  
  4821. Break (Section 10.2.3)
  4822.  
  4823. These annotations are used before a break or continue statement.
  4824.  
  4825. innerbreak
  4826.  
  4827. Break is breaking an inner loop or switch.
  4828.  
  4829. loopbreak
  4830.  
  4831. Break is breaking a loop.
  4832.  
  4833. switchbreak
  4834.  
  4835. Break is breaking a switch.
  4836.  
  4837. innercontinue
  4838.  
  4839. Continue is continuing an inner loop.
  4840.  
  4841. Unreachable Code
  4842.  
  4843. This annotation is used before a statement to prevent unreachable code
  4844. errors.
  4845.  
  4846. notreached
  4847.  
  4848. Statement may be unreachable.
  4849.  
  4850. Special Functions (Apppendix E)
  4851.  
  4852. These annotations are used immediately before a function declaration.
  4853.  
  4854. printflike
  4855.  
  4856. Check variable arguments like printf library function.
  4857.  
  4858. scanflike
  4859.  
  4860. Check variable arguments like scanf library function.
  4861.  
  4862. Appendix E Control Comments
  4863.  
  4864. Error Suppression
  4865.  
  4866. Several comments are provided for suppressing messages. In general, it is
  4867. usually better to use specific flags to suppress a particular error
  4868. permanently, but the general error suppression flags may be more convenient
  4869. for quickly suppressing messages for code that will be corrected or
  4870. documented later.
  4871.  
  4872. ignore
  4873. end
  4874.  
  4875.      No errors will be reported in code regions between /*@ignore@*/
  4876.      and /*@end@*/. These comments can be used to easily suppress an
  4877.      unlimited number of messages, but are dangerous since if real
  4878.      errors are introduced in the ignore ... end region they will not
  4879.      be reported. The ignore and end comments must be matched - a
  4880.      warning is printed if the file ends in an ignore region or if
  4881.      ignore is used inside ignore region.
  4882.  
  4883. i
  4884.  
  4885.      No errors will be reported from an /*@i@*/ comment to the end of
  4886.      the line.
  4887.  
  4888. i<n>
  4889.  
  4890.      No errors will be reported from an /*@i<n>@*/ (e.g., /*@i3@*/)
  4891.      comment to the end of the line. If there are not exactly n errors
  4892.      suppressed from the comment point to the end of the line, LCLint
  4893.      will report an error. This is more robust than i or ignore since a
  4894.      message is generated if the expected number errors is not present.
  4895.      Since errors are not necessarily detected until after this file is
  4896.      processed (for example, and unused variable error), suppress count
  4897.      errors are reported after all files have been processed. The
  4898.      -supcounts flag may be used to suppress these errors. This is
  4899.      useful when a system if being rechecked with different flag
  4900.      settings.
  4901.  
  4902. t
  4903. t<n>
  4904.  
  4905.      Like i and i<n>, except controlled by +tmpcomments flag. These can
  4906.      be used to temporarily suppress certain errors. Then, -tmpcomments
  4907.      can be set to find them again.
  4908.  
  4909. Type Access
  4910.  
  4911. Control comments may also be used to override type access settings. The
  4912. syntax /*@access <type>,+@*/ allows the following code to access the
  4913. representation of <type>. Similarly, /*@noaccess <type>,+@*/ restricts
  4914. access to the representation of <type>. The type in a noaccess comment must
  4915. have been declared as an abstract type. Type access applies from the point
  4916. of the comment to the end of the file or the next access control comment for
  4917. this type.
  4918.  
  4919. Macro Expansion
  4920.  
  4921. The /*@notfunction@*/indicates that the next macro definition is not
  4922. intended to be a function, and should be expanded in line instead of checked
  4923. as a macro function definition.
  4924.  
  4925. Traditional Lint Comments
  4926.  
  4927. Some of the control comments supported by most standard UNIX lints are
  4928. supported by LCLint so legacy systems can be checked more easily. These
  4929. comments are not lexically consistent with LCLint comments, and their
  4930. meanings are less precise (and may vary between different lint programs), so
  4931. we recommend that LCLint comments are used instead except for checking
  4932. legacy systems already containing standard lint comments.
  4933.  
  4934. These standard lint comments supported by LCLint:
  4935.  
  4936. /*FALLTHROUGH*/ (alternate misspelling, /*FALLTHRU*/)
  4937.  
  4938.      Prevents errors for fall-through cases. Same meaning as
  4939.      /*@fallthrough@*/.
  4940.  
  4941. /*NOTREACHED*/
  4942.  
  4943.      Prevents errors about unreachable code (until the end of the
  4944.      function). Same meaning as /*@notreached@*/.
  4945.  
  4946. /*PRINTFLIKE*/
  4947.  
  4948.      Arguments similar to the printf library function (there didn't
  4949.      seem to be much of a consensus among standard lints as to exactly
  4950.      what this means). LCLint supports:
  4951.  
  4952.      /*@printflike@*/
  4953.  
  4954.           Function takes zero or more arguments of any type, an
  4955.           unmodified char * format string argument and zero of
  4956.           more arguments of type and number dictated by the format
  4957.           string. Format codes are interpreted identically to the
  4958.           printf standard library function. May return a result of
  4959.           any type. (LCLint interprets /*PRINTFLIKE*/ as
  4960.           /*@printflike@*/.)
  4961.  
  4962.      /*@scanflike@*/
  4963.  
  4964.           Like printflike, except format codes are interpreted as
  4965.           in the scanf library function.
  4966.  
  4967. /*ARGSUSED*/
  4968.  
  4969.      Turns off unused parameter messages for this function. The control
  4970.      comment, /*@-paramuse@*/ can be used to the same effect, or
  4971.      /*@unused@*/ can be used in individual parameter declarations.
  4972.  
  4973. LCLint will ignore standard lint comments if -lintcomments is used. If
  4974. +warnlintcomments is used, LCLint generates a message for standard lint
  4975. comments and suggest replacements,
  4976.  
  4977. Appendix F Libraries
  4978.  
  4979. Libraries can be used to record interface information. A library containing
  4980. information about the Standard C Library is used to enable checking of
  4981. library calls. Program libraries can be created to enable fast checking of
  4982. single modules in a large program.
  4983.  
  4984. Standard Libraries
  4985.  
  4986. In order to check calls to library functions, LCLint uses an annotated
  4987. standard library. This contains more information about function interfaces
  4988. then is available in the system header files since it uses annotations.
  4989. Further, it contains only those functions documented in the ANSI Standard.
  4990. Many systems include extra functions in their system libraries; programs
  4991. that use these functions cannot be compiled on other systems that do not
  4992. provide them. Certain types defined by the library are treated as abstract
  4993. types (e.g., a program should not rely on how the FILE type is implemented).
  4994. When checking source code, LCLint does include system headers according to
  4995. include directive in the source code, but instead uses the library
  4996. description of the standard library.
  4997.  
  4998. LCLint includes three versions of the C standard library: a "relaxed"
  4999. interpretation of the standard library, a stricter interpretation of the
  5000. standard library, and a library based on what is available on most UNIX
  5001. systems.
  5002.  
  5003. The normal behavior of LCLint is to load the standard library
  5004. (lib/standard.lcd in the release packages). This file is generated by
  5005. running,
  5006.  
  5007.    lclint standard.h -nolib -dump standard
  5008.  
  5009. Strict Library
  5010.  
  5011. A stricter version of the standard library, strict.lcd (generated from
  5012. strict.h) is loaded if the strictlib flag is on. This library is a stricter
  5013. interpretation of the ANSI standard library. It will detect more errors in
  5014. some programs, but is likely to produce many spurious errors for typical
  5015. code.
  5016.  
  5017. The differences between the standard library and the strict library are:
  5018.  
  5019.    * The standard library declares the printing functions (fprintf, printf,
  5020.      and sprintf) that may return error codes to return int or void. This
  5021.      prevents typical programs from leading to deluge of ignored return
  5022.      value errors, but may mean some relevant errors are not detected. In
  5023.      the strict library, they are declared to return int, so ignored return
  5024.      value errors will be reported (depending on other flag settings).
  5025.      Programs should check that this return value is non-negative.
  5026.    * The standard library declares some parameters and return values to be
  5027.      alternate types (int or bool, or int or char). The ANSI standard
  5028.      specifies these types as int to be compatible with older versions of
  5029.      the library, but logically they make more sense as bool or char. In the
  5030.      strict library, the stronger type is used. The parameter to assert is
  5031.      int or bool in the standard library, and bool in the strict library.
  5032.      The parameter to the character functions isalnum, isalpha, iscntrl,
  5033.      isdigit, isgraph, islower, isprint, ispunct, isspace, isupper,
  5034.      isxdigit, tolower, and toupper is char or int in the standard library
  5035.      and char in the strict library. The type of the return value of the
  5036.      character classification functions (all of the previous character
  5037.      functions except tolower and toupper) is bool or int in the standard
  5038.      library and bool in the strict library. The type of the first parameter
  5039.      to ungetc is char or int in the standard library and char in the strict
  5040.      library (EOF should not be passed to ungetc). The second parameter to
  5041.      strchr and strrchr is char or int in the standard library and char in
  5042.      the strict library.
  5043.    * The global variables stdin, stdout and stderr are declared as unchecked
  5044.      variables (see Section 4.2.1) in the standard library. In the strict
  5045.      library, they are checked.
  5046.    * The global variable errno is declared unchecked in the standard
  5047.      library, but declared checkedstrict in the strict library.
  5048.  
  5049. UNIX Library
  5050.  
  5051. The UNIX library, unix.lcd (generated from lib/unix.h) is loaded if the
  5052. unixlib flag is on. This library attempts to capture what a consensus of
  5053. UNIX platforms provide. Unfortunately, UNIX systems vary widely and very few
  5054. are consistent with the ANSI Standard.
  5055.  
  5056. The differences between the standard library and the UNIX library are:
  5057.  
  5058.    * In the UNIX library, free is declared with a non-null parameter. ANSI C
  5059.      specifies that free should handle the argument NULL, but several UNIX
  5060.      platforms crash if NULL is passed to free.
  5061.    * Extra variables, constants and functions are included in the UNIX
  5062.      library. Some declarations are not part of the standard library, but
  5063.      are believed to be available on many UNIX systems. See unix.h for a
  5064.      list of the UNIX-only declarations. Code checked using the UNIX library
  5065.      can probably be ported to some UNIX systems without difficulty. To
  5066.      enhance the likelihood that a program is portable, the standard or
  5067.      strict library should also be used.
  5068.  
  5069. If no library flag is used, LCLint will load the standard library,
  5070. standard.lcd. If +nolib is set, no library is loaded. The library source
  5071. files can easily be modified, and new libraries created to better suit a
  5072. particular application.
  5073.  
  5074. User Libraries
  5075.  
  5076. To enable running LCLint on large systems, mechanisms are provided for
  5077. creating libraries containing necessary information. This means source files
  5078. can be checked independently, after a library has been created. The command
  5079. line option -dump library stores information in the file library (the
  5080. default extension, .lcd[27], is added). Then, -load library loads the
  5081. library. The library contains interface information from the files checked
  5082. when the library was created.
  5083.  
  5084. Header File Inclusion
  5085.  
  5086. LCLint is fast enough that it can be run on medium-size (10,000 line)
  5087. programs without performance concerns. It takes about one second to process
  5088. a thousand source lines on a DEC Alpha. Libraries can be used to enable
  5089. efficient checking of small modules in large programs. To further improve
  5090. performance, header file inclusion can be optimized.
  5091.  
  5092. When processing a complete system in which many files include the same
  5093. headers, a large fraction of processing time is wasted re-reading header
  5094. files unnecessarily. If you are checking a 100-file program, and every file
  5095. includes utils.h, LCLint will have to process utils.h 100 times (as would
  5096. most C compilers). If the +singleinclude flag is used, each header file is
  5097. processed only once. Single header file processing produces a significant
  5098. efficiency improvement when checking large programs split into many files,
  5099. but is only safe if the same header file included in different contexts
  5100. always has the same meaning (i.e., it does not depend on preprocessor
  5101. variable defined differently at different inclusion sites).
  5102.  
  5103. When processing a single file in a large system, a large fraction of the
  5104. time is spent processing included header files. This can be avoided if the
  5105. information in the header files is stored in a library instead. If
  5106. +neverinclude is set, inclusion of files ending in .h is prevented. Files
  5107. with different suffixes are included normally. To do this the header files
  5108. must not include any expanded macros. That is, the header file must be
  5109. processed with +allmacros, and there must be no /*@notfunction@*/ control
  5110. comments in the header. Then, the +noinclude flag may be used to prevent
  5111. inclusion of header files. Alternately, non-function macros can be moved to
  5112. a different file with a name that does not end in .h. Remember, that this
  5113. file must be included directly from the .c file, since if it is included
  5114. from a .h file indirectly, that .h file is ignored so the other file is
  5115. never included.
  5116.  
  5117. These options can be used for significant performance improvements on large
  5118. systems. The performance depends on how the code is structured, but checking
  5119. a single module in a large program is several times faster if libraries and
  5120. +noinclude are used.
  5121.  
  5122. Appendix G Specifications
  5123.  
  5124. Another way of providing more information about programs is to use formal
  5125. specifications. Although this document has largely ignored specifications,
  5126. LCLint was originally designed to use the information in LCL specifications
  5127. instead of source-code annotations. This document focuses on annotations
  5128. since it takes less effort to add annotations to source code than to
  5129. maintain an additional specification file. Annotations can express
  5130. everything that can be expressed in LCL specifications that is relevant to
  5131. LCLint checking. However, LCL specifications can provide more precise
  5132. documentation on program interfaces than is possible with LCLint
  5133. annotations. This appendix (extracted from [Evans94]) is a very brief
  5134. introduction to LCL Specifications. For more information, consult [GH93].
  5135.  
  5136. The Larch family of languages is a two-tiered approach to formal
  5137. specification. A specification is built using two languages -- the Larch
  5138. Shared Language (LSL), which is independent of the implementation language,
  5139. and a Larch Interface Language designed for the specific implementation
  5140. language. An LSL specification defines sorts, analogous to abstract types in
  5141. a programming language, and operators, analogous to procedures. It expresses
  5142. the underlying semantics of an abstraction.
  5143.  
  5144. The interface language specifies an interface to an abstraction in a
  5145. particular programming language. It captures the details of the interface
  5146. needed by a client using the abstraction and places constraints on both
  5147. correct implementations and uses of the module. The semantics of the
  5148. interface are described using primitives and sorts and operators defined in
  5149. LSL specifications. Interface languages have been designed for several
  5150. programming languages.
  5151.  
  5152. LCL [GH93, Tan94] is a Larch interface language for Standard C. LCL uses a
  5153. C-like syntax. Traditionally, a C module M consists of a source file, M.c,
  5154. and a header file, M.h. The header file contains prototype declarations for
  5155. functions, variables and constants exported by M, as well as those macro
  5156. definitions that implement exported functions or constants, and definitions
  5157. of exported types. When using LCL, a module includes two additional files -
  5158. M.lcl, a formal specification of M, and M.lh, which is derived by LCLint (if
  5159. the lh flag is on) from M.lcl. Clients use M.lcl for documentation, and
  5160. should not need to look at any implementation file. The derived file, M.lh,
  5161. contains include directives (if M depends on other specified modules),
  5162. prototypes of functions and declarations of variables as specified in M.lcl.
  5163. The file M.h should include M.lh and retain the implementation aspects of
  5164. the old M.h, but is no longer used for client documentation.
  5165.  
  5166. The LCLint release package includes a grammar for LCL and examples of LCL
  5167. specifications.
  5168.  
  5169. Specification Flags
  5170.  
  5171. These flags are relevant only when LCLint is used with LCL specifications.
  5172.  
  5173. Global Flags
  5174.  
  5175. lcs
  5176.  
  5177. Generate .lcs files containing symbolic state of .lcl files (used for
  5178. imports). By default .lcs files are generated for each .lcl file processed.
  5179. Use -lcs to prevent generation of .lcs files.
  5180.  
  5181. lh
  5182.  
  5183. Generate .lh files. By default, -lh is set and no .lh files are generated.
  5184. Use +lh to enable .lh file generation.
  5185.  
  5186. i <file>
  5187.  
  5188. Set LCL initialization file to <file>. The LCL initialization file is read
  5189. if any .lcl files are listed on the command line. The default file is
  5190. lclinit.lci, found on the LARCH_PATH.
  5191.  
  5192. lclexpect <number>
  5193.  
  5194. Exactly <number> specification errors are expected. Specification errors are
  5195. errors detected when checking the specifications. They do not depend on the
  5196. source code.
  5197.  
  5198. Implicit Globals Checking Qualifiers
  5199.  
  5200. m: -+++
  5201. impcheckedspecglobs
  5202. Implicit checked qualifier on global variables specified in an LCL file with
  5203. no checking annotation.
  5204.  
  5205. m: ----
  5206. impcheckmodspecglobs
  5207. Implicit checkmod qualifier on global variables specified in an LCL file
  5208. with no checking annotation.
  5209.  
  5210. m: ---+
  5211. impcheckedstrictspecglobs
  5212.  
  5213. Implicit checked qualifier on global variables specified in an LCL file with
  5214. no checking annotation.
  5215.  
  5216. Implicit Annotations
  5217.  
  5218. plain: -
  5219. specglobimponly
  5220.  
  5221. Implicit only annotation on global variable declaration in an LCL file with
  5222. no allocation annotation.
  5223.  
  5224. plain: -
  5225. specretimponly
  5226.  
  5227. Implicit only annotation on return value declaration in an LCL file with no
  5228. allocation annotation.
  5229.  
  5230. plain: -
  5231. specstructimponly
  5232.  
  5233. Implicit only annotation on structure field declarations in an LCL file with
  5234. no allocation annotation.
  5235.  
  5236. shortcut
  5237. specimponly
  5238.  
  5239. Sets specglobimponly, specretimponly and specstructimponly.
  5240.  
  5241. Macro Expansion
  5242.  
  5243. plain: +
  5244. specmacros
  5245.  
  5246. Macros defining specified identifiers are not expanded and are checked
  5247. according to the specification.
  5248.  
  5249. Complete Programs and Specifications
  5250.  
  5251. m: -+++
  5252. specundef
  5253.  
  5254. Function, variable, iterator or constant specified but never defined.
  5255.  
  5256. plain: -
  5257. specundecl
  5258.  
  5259. Function, variable, iterator or constant specified but never declared.
  5260.  
  5261. plain: -
  5262. needspec
  5263.  
  5264. There is information in the specification that is not duplicated in
  5265. syntactic comments. Normally, this is not an error, but it may be useful to
  5266. detect it to make sure checking incomplete systems without the
  5267. specifications will still use this information.
  5268.  
  5269. shortcut
  5270. exportany
  5271.  
  5272. An error is reported for any identifier that is exported but not specified.
  5273. (Sets all export flags below.)
  5274.  
  5275. m: ---+
  5276. exportconst
  5277.  
  5278. Constant exported but not specified.
  5279.  
  5280. m: ---+
  5281. exportvar
  5282.  
  5283. Variable exported but not specified.
  5284.  
  5285. m: ---+
  5286. exportfcn
  5287.  
  5288. Function exported but not specified.
  5289.  
  5290. m: ---+
  5291. exportiter
  5292.  
  5293. Iterator exported but not specified.
  5294.  
  5295. m: ---+
  5296. exportmacro
  5297.  
  5298. An expanded macro exported but not specified
  5299.  
  5300. m: ---+
  5301. exporttype
  5302.  
  5303. Type definition exported but not specified
  5304.  
  5305. Appendix H Emacs
  5306.  
  5307. LCLint can be used most productively with the emacs text editor. The release
  5308. package includes emacs files for running LCLint and editing code with
  5309. annotations.
  5310.  
  5311. Running LCLint
  5312.  
  5313. LCLint release includes emacs/lclint.elc that defines an emacs command, M-x
  5314. lclint, for running LCLint. To load this file, add this line to your .emacs
  5315. file:
  5316.  
  5317. (load-file "<directory>/lclint.elc")
  5318.  
  5319. The M-x lclint command is similar to M-x compile, except it jumps to the
  5320. exact column location of the error message, instead of the beginning of the
  5321. line. After typing M-x lclint, you will be prompted for a compile command.
  5322. Enter the command identically to the command that would be used to run
  5323. LCLint from the command line. If errors are found, M-x next-lclint-error
  5324. jumps to the point where the next error was found. (Note, this only works if
  5325. +showcolumn is set to make LCLint include column numbers in error messages.)
  5326.  
  5327. The command can be bound to a key to enable rapid jumping through the error
  5328. messages. For example, to set the key do CTRL-backslash add this line to
  5329. your .emacs file:
  5330.  
  5331.    (global-set-key "^\" 'next-lclint-error)
  5332.  
  5333. Editing Abbreviations
  5334.  
  5335. An additional file, emacs/lclint-abbrevs contains abbreviations for LCLint
  5336. syntactic comments and annotations. If it is loaded, the comment surrounding
  5337. an LCLint annotation will be added automatically. For example, typing "only"
  5338. and a space, will produce "/*@only@*/ ". Abbreviations are provided for each
  5339. LCLint syntactic comment. The abbreviation of /*@null@*/ is nll (not null),
  5340. since it is often necessary to type NULL.
  5341.  
  5342. Abbreviations are loaded and used when a .c or .h file is edited by adding
  5343. these lines to your .emacs file:
  5344.  
  5345. (quietly-read-abbrev-file "<directory>/lclint-abbrevs")
  5346.  
  5347. (setq c-mode-hook (function (lambda nil (abbrev-mode 1))))
  5348.  
  5349. References
  5350.  
  5351. LCLint
  5352.  
  5353. [Evans94] David Evans. Using specifications to check source code. MIT/LCS/TR
  5354. 628, Laboratory for Computer Science, MIT, June 1994.
  5355.  
  5356.      SM Thesis. Describes research behind LCLint, focusing on how
  5357.      specifications can be exploited to do lightweight checking.
  5358.      Includes case studies using LCLint.
  5359.  
  5360. [EGHT94] David Evans, John Guttag, Jim Horning and Yang Meng Tan. LCLint: A
  5361. tool for using specifications to check code. SIGSOFT Symposium on the
  5362. Foundations of Software Engineering, December 1994.
  5363.  
  5364.      Introduction to LCLint. Shows how LCLint is used to find errors in
  5365.      a sample program.
  5366.  
  5367. [Evans96] David Evans. Static Detection of Dynamic Memory Errors. To appear
  5368. in SIGPLAN Conference on Programming Language Design and Implementation
  5369. (PLDI '96), Philadelphia, PA., May 1996.
  5370.  
  5371.      Describes approach for exploiting annotations added to code to
  5372.      detect a wide class of errors. Focuses on checks described in
  5373.      Sections 5-7 of this guide.
  5374.  
  5375. Larch
  5376.  
  5377. [GH93] Guttag, John V. and Horning, James J., with Stephen J. Garland, Kevin
  5378. D. Jones, Andrés Modet, and Jeannette M. Wing, Larch: Languages and Tools
  5379. for Formal Specification, Springer-Verlag, Texts and Monographs in Computer
  5380. Science, 1993.
  5381.  
  5382.      Overview of the Larch family of specification languages and
  5383.      related tools. Includes a chapter on LCL, the Larch C interface
  5384.      language, on which LCLint is based.
  5385.  
  5386. [Tan94] Tan, Yang Meng. Formal specification techniques for promoting
  5387. software modularity, enhancing software documentation, and testing
  5388. specifications. MIT/LCS/TR 619, Laboratory for Computer Science, MIT, June
  5389. 1994.
  5390.  
  5391.      Ph. D. Thesis. Includes presentation of the semantics of LCL and a
  5392.      case study using LCL.
  5393.  
  5394. C
  5395.  
  5396. [ANSI] American National Standard for Information Systems, Programming
  5397. Language, C. ANSI X3.159-1989. (Believed to be identical to ISO/IEC
  5398. 9899:1990).
  5399.  
  5400.      Specification for C programming language. LCLint aims to be
  5401.      consistent with this document.
  5402.  
  5403. [Hat95] Hatton, Les. Safer C: Developing Software for High-integrity and
  5404. Safety-critical Systems. McGraw-Hill International Series in Software
  5405. Engineering, 1995.
  5406.  
  5407.      A broad work on all aspects of developing safety-critical
  5408.      software, focusing on the C language. Provides good justification
  5409.      for the use of C in safety-critical systems, and the necessity of
  5410.      tool-supported programming standards. LCLint users will be
  5411.      interested to see how many of the errors listed as only being
  5412.      dynamically detectable can be detected statically by LCLint.
  5413.  
  5414. [KR88] Kernighan, Brian W. and Ritchie, Dennis M. The C Programming
  5415. Language, second edition. Prentice Hall, New Jersey, 1988.
  5416.  
  5417.      Standard reference for ANSI C. If you haven't heard of this one,
  5418.      you probably didn't get this far (unless you started at the back).
  5419.  
  5420. [vdL94] Van der Linden, Peter. Expert C Programming: Deep C Secrets. SunSoft
  5421. Press, Prentice Hall, New Jersey, 1994.
  5422.  
  5423.      Filled with useful information on the darker corners of C, as well
  5424.      as lots of industry anecdotes and humor. LCLint's reserved name
  5425.      checking is loosely based on the list of reserved names in this
  5426.      book.
  5427.  
  5428. Abstract Types
  5429.  
  5430. [LG86] Liskov, Barbara. and Guttag, John V. Abstraction and Specification in
  5431. Program Development, MIT Press, Cambridge, MA, 1986.
  5432.  
  5433.      Describes a programming methodology using abstract types and
  5434.      specified interfaces. Much of the methodology upon which LCLint is
  5435.      based comes from this book. Uses the CLU programming language.
  5436.  
  5437. Acknowledgements
  5438.  
  5439. John Guttag and Jim Horning had the original idea for LCLint, have provided
  5440. valuable advice on its functionality and design, and been instrumental in
  5441. its development. This work has also benefited greatly from discussions with
  5442. Mike Burrows, Stephen Garland, Colin Godfrey, Steve Harrison, Daniel
  5443. Jackson, Angelika Leeb, Ulana Legedza, Raymie Stata, Yang Meng Tan, and Mark
  5444. Vandevoorde. I especially thank Angelika Leeb for many constructive comments
  5445. on improving the User's Guide, Raymie Stata for help designing and setting
  5446. up the web site and Mark Vandevoorde for technical assistance.
  5447.  
  5448. Much of LCLint's development has been driven by feedback from users in
  5449. academia and industry. Many more people than I can mention here have made
  5450. contributions by suggesting improvements, reporting bugs, porting early
  5451. versions of LCLint to other platforms. Particularly heroic contributions
  5452. have been made by Eric Bloodworth, Jutta Degener, Rick Farnbach, Chris
  5453. Flatters, Huver Hu, John Gerard Malecki, Thomas G. McWilliams, Michael
  5454. Meskes, Richard O'Keefe, and Albert L. Ting.
  5455.  
  5456. LCLint incorporates the original LCL checker developed by Yang Meng Tan.
  5457. This was built on the DECspec Project (Joe Wild, Gary Feldman, Steve
  5458. Garland, and Bill McKeeman). The LSL checker used by LCLint was developed by
  5459. Steve Garland. The original C grammar for LCLint was provided by Nate
  5460. Osgood.
  5461.  
  5462. This research was supported by grants from ARPA (N0014-92-J-1795), NSF
  5463. (9115797-CCR) and DEC ERP. LCLint was developed on DEC 3000/500 and DECmips
  5464. machines provided by Digital Equipment Corporation. This document was
  5465. produced using Pentium(tm) Computers donated by Intel Corporation and
  5466. Microsoft Office(tm) software donated by Microsoft.
  5467.  
  5468. If you are listed here, and want me to add a link to your homepage, send me
  5469. email.
  5470.  
  5471. Footnotes
  5472.  
  5473. 1 Lint is a common programming tool for detecting anomalies in C programs.
  5474. The original lint was developed by S. C. Johnson in the late seventies,
  5475. mainly because early versions of C did not support function prototypes.
  5476.  
  5477. 2 Another way to provide extra information about code is to use formal
  5478. specifications (Appendix G).
  5479.  
  5480. 3 Unlike regular C comments, control comments should not be used within a
  5481. single token. They may introduce new separators in the code during parsing.
  5482.  
  5483. 4 For abstract types whose instances can change value, a client does need to
  5484. know if assignment has copy or sharing semantics (see Section 3.2).
  5485.  
  5486. 5 Does not apply to HTML version. italics.
  5487.  
  5488. 6 The meta-notation, item,+ is used to denote a comma separated list of
  5489. items. For example, /*@access mstring, intSet@*/ provides access to the
  5490. representations of both mstring and intSet.)
  5491.  
  5492. 7 Through the parameter. Modifications using some other variable that has a
  5493. pointer to the location of this parameter are not considered.
  5494.  
  5495. 8 The flag -booltype can be used to select a different name for the boolean
  5496. type. To change the names of TRUE and FALSE, use -booltrue and -boolfalse.
  5497. The LCLint distribution includes an implementation of bool, in lib/bool.h.
  5498. However, it isn't necessary to use this implementation to get the benefits
  5499. of boolean checking.
  5500.  
  5501. 9 This means that theoreticians can prove that no algorithm exists that
  5502. solves the problem correctly for all possible programs.
  5503.  
  5504. 10 This section is largely based on [Evans96]. It semi-formally defines some
  5505. of the terms needed to describe memory management checking; if you are
  5506. satisfied with an intuitive understanding of these terms, this section may
  5507. be skipped.
  5508.  
  5509. 11 This is similar to the LISP storage model, except that objects are typed.
  5510.  
  5511. 12 Except sizeof, which does not need the value of its argument.
  5512.  
  5513. 13 If the storage is not assigned to a reference, an internal reference is
  5514. created to track the storage.
  5515.  
  5516. 14 The full declaration of malloc also includes a null annotation (Section
  5517. 7.2) to indicate that the result may be NULL (as it is when the requested
  5518. storage cannot be allocated) and an out annotation (Section 7.1) to indicate
  5519. that the result points to undefined storage.
  5520.  
  5521. 15 The full declaration of free also has out and null annotations on the
  5522. parameter to indicate that the argument may be NULL and need not point to
  5523. defined storage. According to [ANSI, 4.10.3.2], NULL may be passed to free
  5524. without an error. On some UNIX platforms, passing NULL to free causes a
  5525. program crash so the UNIX version of the standard library (Appendix F)
  5526. specifies free without the null annotation on its parameter. To check that
  5527. allocated objects are completely destroyed (e.g., all unshared objects
  5528. inside a structure are deallocated before the structure is deallocated),
  5529. LCLint checks that any parameter passed as an out only void * does not
  5530. contain references to live, unshared objects. This makes sense, since such a
  5531. parameter could not be used sensibly in any way other than deallocating its
  5532. storage.
  5533.  
  5534. 16 If an exposure qualifier is used (see Section 6.2), the implied dependent
  5535. annotation is used instead of the more generally implied only annotation.
  5536.  
  5537. 17 Strictly, we should also check that the returned observer storage is not
  5538. used again after any other calls to the abstract type module using the same
  5539. parameter. LCLint does not attempt to check this, and in practice it is not
  5540. usually a problem.
  5541.  
  5542. 18 Note that if the parameter is annotated with only, it is not an error to
  5543. assign it to part of an abstract representation, since the caller may not
  5544. use the storage after the call returns.
  5545.  
  5546. 19 That is, the return type is bool, or int if +boolint is used.
  5547.  
  5548. 20 The sef annotation denotes a parameter as side-effect free (see Section
  5549. 8.2.1). By declaring the argument to assert to be side-effect free, LCLint
  5550. will report errors if the parameter to assert produces a side-effect. This
  5551. is especially pertinent if assertions are turned off when the production
  5552. version is compiled. The bool /*@alt int@*/ type specifier for the parameter
  5553. means the parameter type must match either bool or int. Alternate types are
  5554. described in Section 8.2.2.
  5555.  
  5556. 21 To be completely correct, all the macro parameters should be evaluated
  5557. before the macro has any side-effects. Since checking this would require
  5558. extensive analysis for occasional modest gain, it was not considered worth
  5559. implementing.
  5560.  
  5561. 22 Note that functions which do not produce to the same result each time
  5562. they are called with the same arguments should be declared to modify
  5563. internalState so they will lead to errors if they are passed as sef
  5564. parameters.
  5565.  
  5566. 23 The most renown C naming convention is the Hungarian naming convention,
  5567. introduced by Charles Simonyi [Simonyi, Charles, and Martin Heller. "The
  5568. Hungarian Revolution." BYTE, August 1991, p. 131-38]. The names for LCLint
  5569. naming conventions follow the tradition of using Central European
  5570. nationalities as mnemonics for naming conventions. The LCLint conventions
  5571. are similar to the Hungarian naming convention in that they encode type
  5572. information in names, except that the LCLint conventions encode the names of
  5573. accessible abstract types instead of the type of the declaration of return
  5574. value. Prefixes used in the Hungarian naming convention are not supported by
  5575. LCLint.
  5576.  
  5577. 24 Namespace prefixes should probably be described by regular expressions.
  5578. LCLint uses a simpler, more limited means for describing names, which is
  5579. believed to be adequate for describing most useful naming conventions. If
  5580. there is sufficient interest, regular expressions may be supported in a
  5581. future version of LCLint.
  5582.  
  5583. 25 Peter van der Linden estimates that default fall through is the wrong
  5584. behavior 97% of the time. [vdL95, p. 37]
  5585.  
  5586. 26 "Software Glitch Cripples AT&T Network", Telephony, 22 January 1990.
  5587.  
  5588. 27 In earlier versions of LCLint, the default extension .lldmp was used.
  5589. This has been shortened to .lcd.
  5590.